Wednesday, January 26, 2022

Salesforce to Salesforce Connection

Hello All recently I have faced a sitution where I need to call a Salesforce out of the box API call to mitigate mixed DML issue. 

If you are calling the API from the UI when the user already loggedin then you can use userInfo class to get the session id and name the API call. But the problem arise when you need to make the API call from a batch or any async process. 

In the above case you need to crate an user which will login and perform the action. But storing the password in APEX class or in a custom setting is not a very good practice to mitigate this security risk we need 3 different platform capability to store our password securely and. use any Salesforce out of the box or any API in the same or in different Salesforce ORG

At first we need to create a Connected APP. To create a connected app go to Setup and then go tp " APP manager" then click on new connected app 






Now need to create a Auth Provider . From set up go to Auth Provider and click New and select Salesforce as value.





Update the consumer key and secret from the connected app and once saved update the connected App with the Callback URL created. use login and test. URLS you can also use domain specifiv urls. Make sure your defuly scope is "refresh token full"





 Now you have to create a Named credientials for that go to set up -> Named credentials 




Sample code to invoke the API


HttpRequest httpRequest = new HttpRequest(); 

httpRequest.setMethod('GET');        

httpRequest.setEndpoint('callout:SF_to_SF/services/data/v53.0/sobjects/Contact'); 

httpRequest.setBody('{"LastName" : "Express Logistics and Transport"}');

httpRequest.setHeader('Content-Type', 'application/json');

Http http = new Http();  

HttpResponse httpResponse = http.send(httpRequest);

while (httpResponse.getStatusCode() == 302) {

    System.debug('*******' + httpResponse.getBody());

    httpRequest.setEndpoint(httpResponse.getHeader('Location'));

    httpResponse = new Http().send(httpRequest);

}


System.debug('*******======>' + httpResponse.getBody());






No comments:

Post a Comment

Tips on passing Salesforce AI Associate Certification

  🌟 Motivation to Pursue the Salesforce AI Associate Certification 🌟 The world of technology is in a state of perpetual evolution, and on...