Salesforce to Salesforce Integration Using REST API

After learning the REST API, it's always good to do some practice to make the concept more clear. In this Blog, we will Integrate Two Salesforce Org Using REST API. After Successful Integration, when we create an Account (whatever you want) in first (Source) Org, it will automatically get created in second (Target) Org.

Don’t forget to check out: Salesforce REST API

Now here are the Steps for Salesforce to Salesforce Integration Using REST API: 

  • First Create Remote Site Setting in First (Source) Org and Enter Required Fields

download (1)
  • Enter the Target Org Url (Check your Destination Org Instance). For e.g my I used "https://ap4.salesforce.com" Change ap4 according to yours

download (1)
  • Now Create Connected App in your Second (Target) Org

download (3)
  • Enter the Required Details and Enable the Oauth Setting and Enter the Callback URL (shown in the figure) but change the Instance to First Org Instance and Specify the Access (How much access you want to give to other Integrated Org).

download (4)
  • After Creating Connected App Copy the ClientId And ClientSecret To be used in Code.

download (6)
  • Now Go to Connected app "MANAGE " option and change the IP Relaxation to "Relax IP restrictions".IP Restriction can prevent you to get Access Token.

download (5)
  • Now Create an Apex Class in Source Org and Mention the Credentials like username and password of Target Org and then run a future Method to create an Account Asynchronously in Target Org.
  • Make a trigger on insert and call the above class.

 

Responses

  1. Hi am getting below error : Malformed JSON: Expected '{' at the beginning of object , my JSON String is like this :  {"Name":"rest1"}.  please help how to resolve this ? this is in createaccountmethod in the class at line   -- > deserializeResponse deresp2=(deserializeResponse)System.JSON.deserialize(res2.getBody(),deserializeResponse.class);.

  2. Hi Dwarak,

    First check in debug whether you are getting a valid json or not.If not then you can use another way instead of Setting a JsonString manually you need to do this by Json.serialize method which accepts Object.Now you need to create a Account object the set the required field like "Name" then Serialize the Object.
    Account a =new Account();

    a.name="xxx";

    String jsonstring=Json.Serialize(a);

    Then pass this jsonstring in JSon.deserialize method.

    If your Response Json is Valid then Try to use Json Parser instead Of Deserialization get the Reuired Field value using

    JSONParser parser = JSON.createParser(res.getBody());
    while(parser.nextToken()!=null){
    if((parser.getCurrentToken()==JsonToken.FIELD_NAME)&& (parser.getText()=='Name')){
    parser.nextToken();
    accountname=parser.getText();

    }

    Hope this will solve your Problem.

     

     

Comments are closed.

Popular Salesforce Blogs