+ Start a Discussion
Ishwar ShindeIshwar Shinde 

Read timed out error on calling apex rest api through Named Credentail

I am trying to access the apex rest service defined in Salesforce org B from Salesforce org B using named credentails. 

I have performed following steps- 
  • Create Connected App in Salesforce Org B
  • Create Auth. Provider in Salesforce Org A, add consumer secret and key. Copy callback URL. 
  • Update Connected App in Salesforce Org B with Callback URL.
  • Go to Org A --> Create Named Credential --> Select the Oauth --> Select the Auth Provider created earlier  --> Set scope as refresh_token full
     -> Start Authentication Flow on Save --> authorize using Org B
  • Use this Named Cred. in Apex code. 
Code Snippet:
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'application/json');
        Http h = new Http();
        HttpResponse res;
          res = h.send(req);  
        System.debug(' Response '+ res);
        System.debug(' Body '+ res.getBody());

I am getting read timed out error/ Unexpected end of file from server if timed out is set to 120 seconds. 
I have whitlisted all ip addresses in Network setting.
Could you please help what is wrong here?

Thanks in advance.
Best Answer chosen by Ishwar Shinde
Ishwar ShindeIshwar Shinde
I have solved this issue. The url mentioned in named credentails was incorrect. 

All Answers

Raj VakatiRaj Vakati
Can you share the Custom Apex Rest Class from the ORG B  for the endpoint  /apexrest/account . 
  • Check "/services/apexrest/account/" ENDPOINT is valide 
  • CHeck is there any name spaces 
Ishwar ShindeIshwar Shinde
Hi Raj,

Here is the simple code in Org B - 
global class RestRequestHandler {
    global static string getAccountName(String accId){
        List<Account> aList =[Select Id, Name from Account where Id =:accId];
        if(aList.size() > 0){
            return aList[0].Name;
        return 'Account Name';

My goal is to make callout from salesforce to salesforce using Named Credentail. Same functionality is working when I tried with explicit Oauth authetication in code.
Sridhar NarayansaSridhar Narayansa
can you post the screenshot of how you have specified the named credentials? Make sure it is set to Password Authentication.

Also, try by removing this line.
req.setHeader('Content-Type', 'application/json');

Ishwar ShindeIshwar Shinde
Hi Sridhar,

I have added content type after getting an error related to content type.

I am currently using oauth2 Authentication in named credentail. What is the reason for selecting password authentication? [I tried that but getting the same error]

Named Credentail with Oauth 2
Named Principle with Oauth2 protocol
Named Credentail with Password Auth

User-added image
Ishwar ShindeIshwar Shinde
I have solved this issue. The url mentioned in named credentails was incorrect. 
This was selected as the best answer