Sunday, February 27, 2022

Salesforce Composite Connector

Executes a series of REST API requests in a single call. You can use the output of one request as the input to a subsequent request. The response bodies and HTTP statuses of the requests are returned in a single response body. The entire series of requests counts as a single call toward your API limits.


The requests in a composite call are called subrequests. All subrequests are executed in the context of the same user. In a subrequest’s body, you specify a reference ID that maps to the subrequest’s response. You can then refer to the ID in the url or body fields of later subrequests by using a JavaScript-like reference notation.

For example, the following composite request body includes two subrequests. The first creates an account and designates the output as refAccount. The second creates a contact parented under the new account by referencing refAccount in the subrequest body.

{
"compositeRequest" : [{
  "method" : "POST",
  "url" : "/services/data/v54.0/sobjects/Account",
  "referenceId" : "refAccount",
  "body" : { "Name" : "Sample Account" }
  },{
  "method" : "POST",
  "url" : "/services/data/v54.0/sobjects/Contact",
  "referenceId" : "refContact",
  "body" : { 
    "LastName" : "Sample Contact",
    "AccountId" : "@{refAccount.id}"
    }
  }]
}

Step1:

For Consumer key and secret, we need to create a connected app in Salesforce.

Go to Setup -> Build -> Create -> Apps -> Create new in Connected apps


Step2:
 Enable OAuth setting:

Step3:
Save the app.
Step4:
Click on Manage and set Permitted Users as All users may self-authorize, IP Relaxation as Relax IP restrictions.


Step5:
Now get the Consumer id and secret from the custom app:        
 

Step6:
Anypoint Studio: Create your API..(refer Github link)

Use this same key and secret in the Salesforce Composite Connector Configuration:

Sample flow:



Test Data 1:

{
"compositeRequest" : [
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Account",
  "referenceId" : "refAccount",
  "body" : { "Name" : "James Bond  007" }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact",
  "body" : { 
    "LastName" : "David Ross",
    "AccountId" : "@{refAccount.id}"
    }
  }
  ]
}

Test Data 2:
{
"compositeRequest" : [
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Account",
  "referenceId" : "refAccount1",
  "body" : { "Name" : "Michael 1" }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Account",
  "referenceId" : "refAccount2",
  "body" : { "Name" : "Michael 2" }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact1",
  "body" : { 
    "LastName" : "Ross 1",
    "AccountId" : "@{refAccount1.id}"
    }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact2",
  "body" : { 
    "LastName" : "Ross 2",
    "AccountId" : "@{refAccount1.id}"
    }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v51.0/sobjects/Contact",
  "referenceId" : "refContact32",
  "body" : { 
    "LastName" : "Ross 3",
    "AccountId" : "@{refAccount2.id}"
    }
  }
  ]
}

Benefits of Salesforce Composite Connectors:

The Salesforce Composite resources within Salesforce’s REST API can be used to perform complex object interactions that would normally require multiple calls to Salesforce using the real-time API. In certain scenarios, this connector can simplify your flows, reduce the number of API calls to SFDC, and shorten processing time.   

The operations the Composite resources are:

  1. Batch: Batch allows up to 25 separate unrelated actions to be executed in a single call. Each executes independently, and information is not shared between the calls.
  2. SObject Tree: Creates one or more sObject_trees with root records of the specified type. An sObject tree is a collection of nested, parent-child records with a single root record.
  3. SObject Collections: Similar to batch, the SObject Collection resource can reduce round trip calls on groups of objects (up to 200) but requires a common action (e.g. Create, Update, Retrieve, or Delete a group of records). This call has the option to specify a rollback behavior in the case of partial failure.
  4. Composite: This will sequentially execute a series of actions in a single call. The response from one action can be used as an input in the subsequent action.


Code base @GitHub

https://github.com/VISHNUKVM/MuleSoft/blob/main/sfdc-composite-demo.jar

Sources:

https://developer.salesforce.com



HTTP Push Vs Pull

HTTP is the most common methods of data transfer in client server-based architecture. 

But there are two ways to data transfer: HTTP Push and HTTP Pull.

Pull API

In the HTTP pull method, the client sends a request to the server and the server responds to that request (and the connection is closed). The client pulls the data from the server whenever it requires (by creating a new connection). And it keeps doing it over and over to fetch the updated data

The disadvantage of the HTTP pull method is that if clients keep on periodically makes the pull request for updated data, but there are no updates at the server hence, every time the client will get the same result, bandwidth will be wasted and the server will be busy too.

Also, excessive pulls by the clients have the potential to bring down the server.

This is also called "Polling" and it's basically the same as refreshing your inbox every 5 minutes to check for new mail.

As a developer, if you want to find out whether an API has anything new for the user, you just need to call and ask.

Generally you can poll the API anytime, and as often as you like, but some larger applications typically have limits on how often they allow you to call their API.







Push API
To overcome the problem with HTTP pull, an HTTP push was introduced. In the HTTP push method, the client opens a connection to the server by requesting a server only the first time and after that server keeps on pushing back updated content to the client, whenever there’s any.

In this method, the client receives the updated content instantly.

The HTTP push mechanism, leverage the persistent connection i.e. the connection remains constantly active for further communications instead of being closed after a single request and response, just like in HTTP pull.

A much faster way to monitor for updates is to reverse the roles: have the source application send out an update when it has something new. This is called a Push API, and is generally referred to as a HTTP request or a webhook.

A webhook is a specific URL your application gives to another application with the instructions to send the update this way whenever something new has happened. Then all your application has to do, is monitor it's own URL which is much less resource-intensive and nearly instantaneous.








Sources:
https://medium.com


Saturday, February 26, 2022

Database - Important Queries

Sub Query:

A subquery in MySQL is a query, which is nested into another SQL query and embedded with SELECT, INSERT, UPDATE or DELETE statement along with the various operators.

 We can also nest the subquery with another subquery. A subquery is known as the inner query, and the query that contains subquery is known as the outer query. 

The inner query executed first gives the result to the outer query, and then the main/outer query will be performed. MySQL allows us to use subquery anywhere, but it must be closed within parenthesis. All subquery forms and operations supported by the SQL standard will be supported in MySQL also.

Eg: select * from emp_personal where id in (select id from project where skill =:skill);

Join:

 JOINS are used with SELECT statement. It is used to retrieve data from multiple tables. It is performed whenever you need to fetch records from two or more tables.

Eg: SELECT columns  FROM table1   INNER JOIN table2  ON table1.column = table2.column;   

Stored Procedure:

Stored Procedures are created to perform one or more DML operations on Database. It is nothing but the group of SQL statements that accepts some input in the form of parameters and performs some task and may or may not returns a value. 

CREATE or REPLACE PROCEDURE INC_SAL(eno IN NUMBER, up_sal OUT NUMBER)

IS

BEGIN

UPDATE emp_table SET salary = salary+1000 WHERE emp_no = eno;

COMMIT;

SELECT sal INTO up_sal FROM emp_table WHERE emp_no = eno;

END; 

Execute Script:

The script can contain multiple statements.
Statements can be of different types.
No input or output parameters are accepted.
The operation runs any script that does not involve a SQL projection.
Eg:update project set skill='SAP' where id = 104

Execute DDL:

Anypoint Connector for Database (Database Connector) Execute DDL operation enables to execute DDL queries against a database.

CREATE TABLE electronic(
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    description VARCHAR(255),
    price SMALLINT,
    discount TINYINT

Query Single:

Query single operation in the same way as you invoke the Select operation; however, you always get a single result using the Query single operation, regardless of the number of records returned by the actual SQL query.

Bulk insert:

The insert, update, and delete operations can be used for the cases in which each input parameter can take only one value. Alternatively, bulk operations allow you to run a single query using a set of parameters values.

You can avoid unnecessary steps by doing a bulk operation so that:

  •     The query is parsed only once.
  •     Only one database connection is required since a single statement is executed.
  •     Network overhead is minimized.
  •     RDBMS can execute the bulk operation automically.

Query:

insert into project(id,skill,proj_name) values (:eid,:tech,:proj)

payload: [

     {
         "eid"111,
         "tech":"Tibco",
         "proj""Tata"
     },
     {
         "eid"112,
         "tech":"DevOps",
         "proj""Tata"
     },
     {
         "eid"113,
         "tech":"SFDC",
         "proj""Amazon"
     }
 ]
 




GitHub:

https://github.com/VISHNUKVM/MuleSoft/blob/87b403429b94bb93f197d02bc69c31d09fdad982/db-sub-query.jar


References:

https://docs.mulesoft.com
https://www.javatpoint.com
https://www.geeksforgeeks.org







How to setup SSL Key Store in Mule 4

 SSL stands for Secure Socket Layer.
  • - supports Fortezza algorithm.
  • - 3.0 version.
  • - Message digest is used to create master secret.
  • - Message Authentication Code protocol is used.
  • - Is complex than TLS(Transport Layer Security).
  • - Is less secured as compared to TLS(Transport Layer Security).

Important Key terms:
  • SSL - Secure Socket Layer
  • TLS - Transport Layer Security
  • Public Key Infrastructure (PKI)
  • CA - Certificate Authority

Step1: 
Generate Key Store by using following command
C:\Program Files\Java\jdk1.8.0_321\bin>keytool -genkeypair -keystore D:\KeyLocation\keystore.jks -keypass mulesoft -storepass mulesoft -keyalg RSA -sigalg SHA1withRSA -keysize 1024 -alias mule -ext SAN=DNS:localhost,IP:127.0.0.1 -validity 9999
Note: mulesoft is password

C:\Program Files\Java\jdk1.8.0_321\bin>

Open the Key Store by using keystore application. Download from here..

Step2:
Place the keystore.jks file into src/main/resources

Step3:
Configure HTTPS connectore
General>port>8082
TLS>Type>JKS,Path>keystore.jks,Key password=mulesoft,password=mulesoft

GitHub-Code

http.port=8081
https.port=8082
http.private.port=8091
https.private.port=8092



Note: If you are testing from Postman, then disable SSL certificate verification
Postman>File>Settings

https://localhost:8082/Hello


Thanks for reading.



SSL Vs TLS

 Difference between SSL and TLS



SSL - stands for Secure Socket Layer 

TLS - stands for Transport Layer Security. 

These protocols are used to provide security between web browsers and web servers. 




Source: https://www.geeksforgeeks.org/



Data Weave 2.0 - Useful Tips & Techniques

DataWeave (DW) has been part of the MuleSoft family from v3.7.0.

Below listed ones are the few tips and best practices which could be useful in DW.
There are many operators in dataweave which can make our life easy espically for complex dataweave transformation.

DataWeave-Playground is a online tool to experiment the DataWeave stuff without doing it from Anypoint Studio.

URL: I like it, please use the playground for better learning..


Sample Payload:
{
    "name": "Sravan Kumar",
    "age": 30,
    "profession": "Mulesoft Developer",
    "date": "2022-04-21T10:03:45-04:00",
    "comment": "Special characters. "
}

1. Dealing special characters ($)

If you have some special character (e.g. “, $ etc) in your payload. Here is how you can deal them in dataweave using “\”.
%dw 2.0
output application/json
---
{
    name: payload.name,
    age: payload.age,
    profession: payload.profession,
    date: payload.date,
    comment: payload.comment ++ "\$\$ handling special character \$\$"
}

2. Pattern matching

If you want to evaluate that payload.name has any number in it.?
Use regex..it will return true or false only.

%dw 2.0
output application/json
---
{
    name: payload.name,
    age: payload.age,
    profession: payload.profession,
    date: payload.date,
    comment: payload.comment ++ "\$\$ handling special character \$\$",
    "regex_number_check" : payload.name contains /\d+/

}



3.Comments - Useful for easy reading the DW

 // Single line comment like JAVA

/*
Multiline comments
*/

4. Null check - is very useful.

isEmpty(payload.name) - returns true if name is null, else false


5. Add/update a key/s of an object

If you want to add or update the value of key of an object. Here is written small snippt..
If the key is not present in the payload, then it would add a new key to the object otherwise it would update the existing key’s value


Remove key/s of an object 
%dw 2.0
output application/json
---
payload -- ["date","comment"]

6. Use custom dataweave modules for re-usability across the organization. 
Create a file under => src/main/resources/dw/common.dwl
%dw 2.0
fun getUpdatedsting(value:String) = "Welcome" ++" " ++ value

DW-Transformation
%dw 2.0
output application/json
import dw::common

---
{
    name: payload.name,
    age: payload.age,
    profession: payload.profession,
Welcome : common::getUpdatedsting("Jai"),
    comment: payload.comment 
}


Ref: