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
AmulAmul 

How can we access REST Api build in Salesforce Developer Org into Another SFDC Org

I have written this REST class in SFDC developer Org. And I am able to access this REST callout thorugh workbench.developerforce.com using this URL

 

REST URL= 

/services/apexrest/StakkonForce/v1.0/MYCALL/getAccountRecords

===================================================================================================

//Apex Class:

 

@RestResource(urlMapping='/v1.0/MYCALL/*')
global without sharing class MyRestWs{

@HttpGet
global static List<Account> getListOfAccount() {
RestRequest request = RestContext.request;
RestResponse response = Restcontext.response;
String operation = request.requestURI.substring(request.requestURI.lastIndexOf('/getAccountRecords')+1);
String resource = operation.split('/').get(0);
list<Account> lstAccount=new list<Account>();
if(resource == 'getAccountRecords') {
lstAccount= [ Select ID, Name, Phone, BillingState from Account limit 100];
}
return lstAccount;

}

}

===========================================================================================

 

I wanted to Access this above REST call out from different sfdc Developer Org.

 

Please provide me some sample code And Steps.

Thanks in advance.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
HareHare

All Answers

digamber.prasaddigamber.prasad

Hi,

 

I can give you code for sure. But to access this rest endpoint from other salesforce org, you will need session Id of one of user on org1. How will you accomplish this?

digamber.prasaddigamber.prasad

Hi,

 

You can use below code snippet to try this:-

 

public class AnalyticsAPIConnector{
   
    public static string runReportSync(string reportId){
   
        Http http = new Http();
        HttpRequest httpReq = new HttpRequest();
        HttpResponse httpRes = new HttpResponse();
       
        httpReq.setMethod('GET');
        httpReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId()); //please modify it to pass valid session Id
        httpReq.setEndpoint(
            URL.getSalesforceBaseUrl().toExternalForm()+
            '/services/apexrest/StakkonForce/v1.0/MYCALL/getAccountRecords'
        );
       
        httpRes = http.send(httpReq);
       
        return httpRes.getBody();
   
    }
}

 

AmulAmul
Thanks for your quick reply.

But can you please let me know can we access this using SFDC OAuth2.0

using access Token?

For OAuth we need to have Security key and Pass code.
AmulAmul

Image

 

OAuth

 

Please see this 

 

How can we access using thi API OAuth

 

API (Enable OAuth Settings)

Consumer Key
3MVG9ahGHqp.k2_xZ9rc0t3g_Oe1o.ILdv9Ulc8ur4PWz2bRUX30P41K9J9GbZLcz0WX5gVKMzzvPnPh90NeP
Consumer Secret
6012714673031319113
 

 

Pat PattersonPat Patterson

Hi Amul,

 

Read the article on Salesforce as an Identity Provider for Customer Portal - you can have users in one org logging into another, either as portal/community users, or internal users. In either case, you get an access token (accessible via AuthToken.getAccessToken) in the 'target' org that you can use in REST calls to the 'source' org.

 

Cheers,

 

Pat

AmulAmul

Thank you so much Pat.

HareHare
This was selected as the best answer
AmulAmul
Awesome jitendra
AmulAmul
Thank you hareesh