function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RudraRudra 

Integration between two salesforce orgs using rest api intregration

I have a requirement to integrate between two salesforce applications(orgs). The integration I need to used is rest api integration.
Please help me by giving me a head start how should I begin.
To be more specific my requirement is to exectue a search functionality in one salesforce org which fetchs and displays data present another salesforce org using rest api integration.


Avidev9Avidev9
I think this should pretty straight forward, since you are looking to integrate two different orgs you may want to Write Apex REST Webservice in our Source(where you want to search) org and consume the same using http callouts in the destination ORG.

There are basically three parts
  1. Authentication : You can use OAuth
  2. Writing Apex Webservices Class : This is needed for all custom search logic you want http://blogforce9.blogspot.in/2013/09/salesforce-rest-webservices-part-i.html
  3. Call the Rest Webservice from Destination org http://blogforce9.blogspot.in/2014/02/tooling-api-js-remoting-jsrender.html
I have also linked you some related blogs, they may not give you the straigth solution but will give you an Idea on how to proceed
farukh sk hdfarukh sk hd
Hi,

This  will help you for sure.Here rest api integration is performed by taking example of two salesforce system.

https://www.sfdc-lightning.com/2019/01/salesforce-rest-api-integration.html

Please mark this as best answer if this helps you.
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Rudra,

Steps-:
1)Go to setup in source org->search->App Manager in quick find->Create Connected App.
2)After this  Develop  Apex RestService in the Same Source Org.
Sample code: In this, I am Fetching Account.
@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {
    @HttpGet
    global static Account getRecord() {
        RestRequest req=RestContext.request;
        RestResponse res=RestContext.response;
        String urlId=req.requestURI.subString(req.requestURI.lastIndexOf('/')+1);
        Account accountList=[SELECT Id,Name,Phone,Description,Rating FROM Account WHERE Id =:urlId];
        return accountList;
    }
}
3)Now, We use the above Apex Rest Service in Target Salesforce Org.
Sample code-:In this I am fetching accounts from one org to another.
public class MyFirstRestApi {
    public static void getAccount(){
        HttpRequest req=new HttpRequest();
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        req.setMethod('POST');
        String CLIENT_ID = '3MVG9fe4g9fhX0E7c0s6z5_ojYYqdlgULEYYd4azO4hjX2hlWYb4jaNMUX.fYNikWLjDEmG3YMURayF.ySmbh';
        String CLIENT_SECRET = '379438EC996D2345F3632774AA40F7DF0A63D004C02C9781CDE6DA6F1623D92A';
        String USERNAME = 'nainsi.verma@cloudanalogy.com';
        String PASSWORD = 'Nainsi420@ujaA865VERblPRkzjb1TOeetE';
        
        req.setBody('grant_type=password' + '&client_id='+CLIENT_ID + 
                    '&client_secret='+CLIENT_SECRET + '&username='+USERNAME + '&password='+PASSWORD);
        Http http=new Http();
        HttpResponse res=new HttpResponse();
        res=http.send(req);
        System.debug('response-->: '+res.getBody());
        Oauth objAuthInfo=(Oauth)JSON.deserialize(res.getBody(), Oauth.class);
        if(objAuthInfo.access_token!=null){
            HttpRequest req1=new HttpRequest();
            req1.setEndpoint('https://cloudanalogy-2dd-dev-ed.my.salesforce.com/services/apexrest/Account/');
            req1.setMethod('GET');
            req1.setHeader('Authorization','Bearer '+objAuthInfo.access_token);
            Http http1=new Http();
            HttpResponse res1=new HttpResponse();
            res1=http1.send(req1);
            System.debug('Status -->: '+res1.getStatusCode());
            System.debug('Account Info-->: '+res1.getBody());
            
        }
    }
    public class Oauth{
        public String access_token{get;set;}
        public String instance_url{get;set;}
        public String id{get;set;}
        public String token_type{get;set;} 
        public String issued_at{get;set;}
        public String signature{get;set;}
    }  
}


You can take references from this link.
http://amulhai.blogspot.com/2015/11/salesforce-to-salesforce-integration.html

If you find your Solution then mark this as the best answer. 


Thank you!

Regards 
Suraj Tripathi