Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search
    Integrating One Salesforce Org to Another Saleforce Org Using REST API

    Integrating One Salesforce Org to Another Salesforce Org Using REST API

    Avnish Yadav Sep 12, 2018
    48,680  Views

    Hello guys,

    Today, I will show you how you can integrate one Salesforce org to another Salesforce Org using REST API. For this, you will need the following things –

    1. Source Org (Salesforce Org)
    2. Target Org (Salesforce Org)

    STEP BY STEP INTEGRATION of Source and Target Salesforce Org-

    Step 1 – Create a Connected App in Target Org.

    By going, Setup-> Create->Apps OR (Search “Apps” in Quick Find Box).

    Click on New and fill the details as per your requirements but your callback URL looks like this-

    https://<domain>.my.salesforce.com/services/oauth2/callback

    After clicking on Save you will see the Client Id and Client Secret key (Save that, we will need it for Integration)

    Step 2 – Create an Apex Class in Target Org to retrieve 10 Accounts from Target Org.

    Go to Developer Console and Create New Apex Class.

    @RestResource(urlMapping='/v1/getAccounts/*')
    global with sharing class FetchAccount {
        @HttpGet
        global static list<account> fetchAccount(){
            RestRequest req = RestContext.request;
            RestResponse res = Restcontext.response;
            List<account> listAccount =[Select Id , Name from Account LIMIT 10 ];
            return listAccount ;
        }
    }

    Step 3 – Create a Remote Setting on Source Org.

    Search “Remote Site Setting” in Quick find Box and Enter Remote Site URL of your Target org, the format is like that –

    https://<domain>.my.salesforce.com

    Step 4 – Create an Apex Class in Source Org, which will callout Apex class of Target Org and return you 10 Accounts.

    Here is the code:

    public class GetAccountUsingRESTAPI {
        private final String clientId = 'XXXXXXXXXXXXXXXX';
        private final String clientSecret = 'XXXXXX8XXX';
        private final String username = 'SalesforceUserName';
        private final String password = 'Password';
        public class deserializeResponse
        {
            public String id;
            public String access_token;
        }
        public String ReturnAccessToken (GetAccountUsingRESTAPI acount)
        {
            String reqbody = 'grant_type=password&amp;client_id='
                +clientId+'&amp;client_secret='
                +clientSecret+'&amp;username='
                +username+'&amp;password='+password;
            Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setBody(reqbody);
            req.setMethod('POST');
            req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
            HttpResponse res = h.send(req);
            deserializeResponse response = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
            system.debug('@@@@access_token@@'+response );
            return response.access_token;
        }
        public static list<account> callGetAccount()
        {
            GetAccountUsingRESTAPI acount1 = new GetAccountUsingRESTAPI();
            String accessToken;
            accessToken = acount1.ReturnAccessToken (acount1);
            list<account> ListAccount=new List<account>();
    
            if(accessToken != null) {
                String endPoint = 'https://avnish1.salesforce.com/services/apexrest/v1/getAccounts/';
                Http h2 = new Http();
                HttpRequest req1 = new HttpRequest();
                req1.setHeader('Authorization','Bearer ' + accessToken);
                req1.setHeader('Content-Type','application/json');
                req1.setHeader('accept','application/json');
                req1.setMethod('GET');
                req1.setEndpoint(endPoint);
                HttpResponse res1 = h2.send(req1);
                
                String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
                system.debug('@@@RESPONSE@@'+trimmedResponse);
                JSONParser parser = JSON.createParser(res1.getBody());
                set<account> accList=new set<account>();
                
                while (parser.nextToken() != null) {
                    if((parser.getCurrentToken() == JSONToken.FIELD_NAME) ) {
                        Account acc;
                        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id')) {
                            parser.nextToken();
                            String sId= parser.getText();
                            acc=new Account();
                            acc.Id=sId;
                            system.debug('Id@@@' + sId);
                            parser.nextToken();
                            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Name')) {
                                parser.nextToken();
                                string sName= parser.getText();
                                acc.Name=sName;
                                system.debug('Name@@@' + sName );
                            }
                        }
                        accList.add(acc); 
                    }
                    accList.remove(null);
                }
                ListAccount.AddAll(accList);
                system.debug('AccountList@@@@'+Json.serialize(ListAccount));
            }
            return ListAccount;
        }
    }

    In this code you will need to replace the following things:-

    • client
    • clientSecret
    • username
    • password

    Also, Change the Endpoint in method ReturnAccessToken() in which org you are trying to log in. means where you have created your Connected Appreq.setEndpoint(‘https://login.salesforce.com/services/oauth2/token’);

    Also, Change the Endpoint in method callGetAccount() in which org you are trying to access all the Contacts. means where you have developed your Apex RestService

    String endPoint = ‘https://avnish1.salesforce.com/services/apexrest/v1/getAccounts/’;

    Now call the function “callGetAccount()”. Feel free to ask questions in a comment box.

    Thanks.

    Happy Coding.

    Categories: Salesforce integration
    Tagged: Callback URL, Client ID, Client Secret Key, Developer Console, Integration, oAuth2, Quick FindBox, Remote Site Settings, Remote Site URL, Rest API, Salesforce Account, Salesforce Accounts, Salesforce Code, Salesforce Integration, Salesforce Org, Tokens

    Get listed your company

    Have an innovative Salesforce solution that delivers faster, smarter results?
    Join the Marketplace

    [adinserter block=”16″]

    best salesforce consultants
    best salesforce consultants
    best salesforce consultants
    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants
    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants
    salesforce consultants
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.