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
Alexander KrykunAlexander Krykun 

How to invoke rest resource from another org

I have 2 orgs. I want to retrive particular account records from another org to my current org, using rest api 
I'm using custom domain name on second org.
Its look like https://nara-narayana-dev-ed.my.salesforce.com/

 

On my current org code that respons for creating request looks like 

Httprequest req=new httprequest();
        String domainName='nara-narayana-dev-ed-c.eu6.visual.force.com';

        String endPointURL='https://'+domainName+'/services/apexrest/Account/0015800000FvETO';
        req.setendpoint(endPointURL);
        req.setHeader('Content-Type', 'application/xml; charset=utf-8');
        req.setmethod('GET');
        req.setHeader('Authorization','Bearer '+ UserInfo.getSessionId());
        Http http = new Http();
        HTTPResponse res = http.send(req);
         String response=res.getbody();
        System.debug('****************res.getStatusCode();'+res.getStatusCode());
        System.debug('****************res.getbody();'+res.getBody());

Rest resourse on second org looks like:(for simplicity, its now returns string 'hello')

@RestResource(urlMapping='/Account/*')
global with sharing class AccountManager {

    @HttpGet
    global static String getAccountById() {
        RestRequest req = RestContext.request;
        /*String accId = req.requestURI.substring(
                req.requestURI.lastIndexOf('/') + 1);
        Account result = [
                SELECT Name
                FROM Account
                WHERE Id = :accId
        ];*/
        return 'hello';
    }

    @HttpPost
    global static String createAccount(String name) {
        Account acc = new Account(
                Name = name);
        insert acc;
        return acc.Id;
    }
}
 

As a result i'm getting [{"errorCode":"NOT_FOUND","message":"Could not find a match for URL /Account/0015800000FvETO"}]
in debug log and status code is 404. So I assume its something wrong with endpoint url. How to make it correct?

Also I have added this url https://nara-narayana-dev-ed.my.salesforce.com  to Remote site settings

 

pconpcon
To start with, you'll want to use the instance name provided by the login method for this, not the my url (ie 'na3.salesforce.com').  Secondly, you will not be able to use the session Id from the current org to make a call to your second org.  The session does not exist on your other org.  You will have to either login and get a session id, or do an oauth login, store the auth code and use that [1].

[1] http://blog.deadlypenguin.com/blog/2016/07/05/oauth-flow-for-service-users-in-salesforce/
Alexander KrykunAlexander Krykun
"To start with, you'll want to use the instance name provided by the login method for this, not the my url (ie 'na3.salesforce.com". 
Can you explain this more clearly please, what url should I use? What is login method?
pconpcon
You can either use the oAuth login flow and store that or you can make a soap login.  I don't have an example of making the soap login via Salesforce, but here it is via bash and cURL [1].  The result will have a variable "serverUrl" that you can use to determine what the hostname is.  This will also have a variable "sessionId" that is your session id on your other host.

[1] https://gist.github.com/pcon/54e8b0d7518d8fe42c79#file-bashrc_sfdc-sh-L55
{tushar-sharma}{tushar-sharma}
You can use Postman to do this. And share the URL with others so they can follow the same.
https://newstechnologystuff.com/2020/05/27/test-salesforce-rest-api-using-postman/

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/