Integration in salesforce using Rest API -Part2
Raptbot Technologies Private Limited
Elevate Your Business with Raptbot: Your Premier Salesforce Partner for Innovation, Integration, and Unmatched Expertise
(Custom Implementation)
Introduction:
Salesforce to Salesforce integration using REST API is a powerful way to connect two Salesforce orgs to share data seamlessly. This approach leverages the REST API provided by Salesforce to allow for real-time data exchange, ensuring that both systems remain in sync. Whether you're integrating two divisions within the same company or connecting to an external partner, understanding how to effectively implement this integration can significantly enhance your business processes and data accuracy.
Steps to Implement Salesforce to Salesforce Integration Using REST API
Step-1: Create a Connected App in target Org.
Connected App: ? Connected app tells Salesforce which authentication protocol—SAML, OAuth, and OpenID Connect—the external app uses, and where the external app runs. Salesforce can then grant the external app access to its data, and attach policies that define access restrictions, such as when the app’s access expires.
First, you need to set up a Connected App in the target Salesforce org to enable OAuth authentication. This will allow the source org to securely connect to the target org.
Setup-> App Manager -> Connected Apps->New
Call Back URL We can give Call Back URL in step two as of now we are in step one can use some dummy URL.
o?? In the Selected OAuth Scopes you can either give full access or according to your organization requirement.
After save you will redirect to one page where you have to provide one received OTP in your email. After clicking on Manage Consumer Details you will see the?Client Id and Client Secret key.?
Step-2:? Create an Auth Provider in source Org.
Use the Client ID and Client Secret to get an access token from the target org.
Auth Provider: Represents an authentication provider (auth provider). An auth provider lets users log in to Salesforce from an external service provider such as Facebook, Google, or GitHub. This type extends the Metadata type and inherits its?full Name?field.
After clicking on save you will get Callback URL put this URL in target org connected app.
?
Step-3: ?Create Name Credential in the source org.
Name Credential: A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. To simplify the setup of authenticated callouts, specify a named credential instead of a URL as the callout endpoint.
Type of Authentication Protocol:
o?? No Authentication
o?? OAuth 2.0
o?? Password Authentication
o?? Aws Signature Version 4
o?? JWT
o?? JWT Token Exchange
In the URL section give Domain Name of Target ORG.
SET UP => QUICK FIND BOX => Domain Name
领英推荐
Step 4:? Create a HTTP callout class from source org.
public class DemoOutbpundIntegration {
//Get Account and related contact from target org.
??? public static void getAccountFromTargetOrg() {
???? ?HttpRequest req = new HttpRequest();
??????? req.setEndpoint('callout:AccountCred/services/apexrest/AccountSync?Industry=Energy');
??????? //AccountCred is a name credential which we have use to set Endpoint
?
??????? req.setMethod('GET');
??????? req.setHeader('Content-Type', 'application/json');
??????? req.setHeader('Accept', 'application/json');
???????
??????? Http http = new Http();
??????? HttpResponse response = http.send(req);
??????? if(response.getStatusCode()== 200){
?????????? System.debug('Response body:' + response.getBody());
?????????? }
??? }
}
Step 5: Create a rest apex in target org.
??? @RestResource(urlMapping='/AccountSync/*')
global class DemoInboundIntegration {
??? global class AccountWrapper{
??????? global List<Account> accList;
??????? global List<Contact> conList;
??? }
@HttpGet
??? global static AccountWrapper getAccounts(){
??????? Map<String, String> params = RestContext.request.params;
??????? String accIndustry = params.get('Industry');
accId.add(acc.Id);??
??????? }
??????? List<Contact> contacts = [SELECT Id, LastName from contact where accountId IN:accId];
??????? AccountWrapper accWrapper = new AccountWrapper();
??????? accWrapper.accList = accounts;
??????? accWrapper.conList = contacts;
??????? return accWrapper;
??? }
}
Conclusion:
Integrating Salesforce to Salesforce using REST API opens up numerous possibilities for seamless data sharing and process automation between different Salesforce orgs. By following the steps outlined above, you can set up a secure, efficient, and effective integration that leverages standard Salesforce features and avoids the need for complex middleware solutions. This integration approach enhances data accuracy, improves operational efficiency, and enables real-time synchronization, ultimately leading to better business outcomes.
Salesforce Developer | Programming enthusiasts | Apex | LWC | Flow | OmniStudio| Java | SQL | DSA | Database
2 个月Very informative
Founder | Salesforce Technical Architect | Driving Scalable Solutions @ Raptbot Technologies
2 个月Thanks for sharing Er. Nilam Kumari