i have two org, first org i have written code for GET method, Second org i am calling a the rest api GET method from javascipt code. in the calling org i dont get any response but it response with failure. i have also setup the remote site url on org 2, still it doesn't work. i cannot figure out the reason. Can you please help?

below code in Org 1:

global class getAccountAPIrecords {

    global static List<Account> doGet()
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        List<Account> lstAcc = new List<Account>();

            lstAcc = [select name, Phone from Account];
            return lstAcc;
        catch(Exception ex)
            system.debug('exception error :' + ex.getMessage());
            RestContext.response.addHeader('Content-type', 'text/plain');
            RestContext.response.responseBody = blob.valueOf('Error');
            return lstAcc; 

visualforce page on Org 2:

<apex:page controller="AccountController" applyBodyTag="true" docType="html-5.0">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
        <script src="/soap/ajax/32.0/connection.js" type="text/javascript">
        <script src="/soap/ajax/32.0/apex.js" type="text/javascript">

        <script type='text/javascript'>
            function callApi()
                  var result = sforce.connection.login("<username>", "<password><securityTokenofThisOrg>");
                  sforce.connection.init('{!<sessionIdOfThisOrg>}', 'https://ritz-callout-dev-ed.my.salesforce.com/services/apexrest/GetAccountDetails');

                    url: "https://ritz-callout-dev-ed.my.salesforce.com/services/apexrest/GetAccountDetails",
                    requestHeaders: { "Content-Type":"application/json"},
                    method: "GET",
                    onSuccess : function(response){
                    onFailure : function(response){
                        console.log("Failed :" + response)
                        alert("Failed :" + response)


            <apex:commandButton value="Call API"  style="font-size: 16px;" onclick="callApi();"></apex:commandButton>

Controller on org 2:

public class AccountController {
    public string sessionId{get;set;}
    public ApexPages.StandardController stdCntrlr {get; set;}
    //ApexPages.StandardController controller
    public AccountController(){
        //stdCntrlr = controller;
        sessionId = UserInfo.getSessionId();
        system.debug('sessionId :' + sessionId);

  • Please include the error messages verbatim, thanks!
    – glls
    Aug 10 '18 at 11:19
  • @glls i don't get any response, no error Aug 10 '18 at 11:56

Both orgs need to have the other org's endpoint listed as trusted sites in CORS (Cross Origin Request Sharing) and probably in Remote Site Settings too. CORS is required for JS functionality between two different Salesforce endpoints. You'll find the CORS settings under Security.


In response to comments...

I see two things here. First, since you are initiating this using JS, I don't think you understand the importance of using CORS. You will want have both sites listed in CORS as well.

The next thing is probably what is causing your biggest issue. As you are passing a SessionId, you will need that Session to be for a specific integration user that your Rest Resource is runing as. If that's what you're doing, if I'm not mistaken, I believe that user will need to be included as having a log-in for the other org as well. You will want to use FrontdoorJsp to handle the login when you have a SessionId. You cannot do this for just "any user". The link I've provided is a KB article with a link to complete instructions and a tutorial on how to set this up.

  • Listed the endpoint for both orgs under Remote Site Setting. still it doesn't work Aug 13 '18 at 5:28
  • @RitzArlekar See my edited answer. I think this should solve your issues to get you up and running.
    – crmprogdev
    Aug 13 '18 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.