Salesforce to Salesforce Integration Using REST API

After learning REST API, it’s always good to do some practical 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.

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 (2)

  • 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 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. You can refer Here for Code text File.
  • Make a trigger on insert and call the above class.Refer here for Trigger code file.

 

3 Comments
  1. Dwarak 2 years ago

    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. Salesforce | Suraj Author
    Suraj 2 years ago

    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.

     

     

  3. Salesforce | Louis
    Louis 2 years ago

    Very well described blog about salesforce integration… Best resources for Salesforce Integration Services.

Leave a Reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?