We are trying to migrate data from one Salesforce org to other using REST web service. Created REST service in one org trying use that service from other org but getting error "System.HttpResponse[Status=Unauthorized, StatusCode=401]".

Http Callout :

String username = correct usernme;
String password = Correct password;

Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'Basic' +

HttpRequest req = new HttpRequest();
//req.setHeader('Authorization', authorizationHeader);
//req.setHeader('content-type', 'application/json');  
Http htp = new Http();
HttpResponse resp = htp.send(req); 

How should i call one Salesforce rest web service from another Salesforce org?


2 Answers 2


This can easily be achieved in a few steps:

  1. Create a Connected App on the Remote org.
  2. Create an Auth. Provider in your local org, using Salesforce Authentication.

    • In your Auth. Provider you will need to add elements from your Connected App.

      • Client Key
      • Client Secret
    • Save your Auth. Provider and Copy the "Callback Url" at the bottom of the view.

    • Paste the Callback Url to the Connected App in the remote org.
  3. Create a Named Credential in your local org.

    • URL: Url of the remote org
    • Identity Type: Named Principle
    • Authentication Protocol: OAuth 2.0
    • Authentication Provider: Auth. Provider created above
    • Scope: refresh_token full
    • Start Authentication Flow on Save: TRUE

NOTE : There will be a few minutes delay between availability of your end point after you save the Callback Url to the Connected app.

  1. Authenticate your Named Credential

Once you have authenticated your Named credential, you can use the named credential to make your callouts to the remote organization. An example of the callout using the named credential would look like:

HttpRequest feedRequest = new HttpRequest();
Http http = new Http();
HTTPResponse feedResponse = http.send(feedRequest);

What you do with your response you can handle in your own way. This should get you on your way. Good luck!

Links for additional Help:

Blog: https://www.jitendrazaa.com/blog/salesforce/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code/

SF Docs:

Named Credentials

Auth. Provider

Merge Fields In Callouts With Named Credentials


You cannot use Basic authentication to connect another salesforce org.

There are two ways you can connect to salesforce org either create a remote application and get the client and secret key from the target system, or try to use soap login methods in the rest api.

Please find the below sample snippet.

HttpRequest request = new HttpRequest();
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' +uname + '</username><password>' + password+'</password></login></Body></Envelope>');
        httpresponse res = new http().send(request);

Please send the valid user name and password+Token, you will recieve session id and you can make another request using the session.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .