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
Neha Patil 20Neha Patil 20 

using named credential while invoking apex rest

Hello Experts,

I want someone to put some light to teh new feature introduced by salesforce Named credential

My scenario is somethign like this:
1.I have a rest based webservice in target org which i am invoking through source org
2.I have created named credential in source org as shown below
User-added image
3. Further in my code that invokes the webservice i have written the below satatements:
HttpRequest req= new HttpRequest();

String reqBody='{"vEmail":"'+email+'",'+'"vSubject":"'+subject+'",'+'"vDate":"'+dt+'"}';
		Http http=new Http();
		HttpResponse res= http.send(req);

However this gives error as Session expired or invalid session id. How do i get the session id if i want to use named credential in my code?
Any help would be appreciated

Adam Drissel 1Adam Drissel 1
Neha, did you find the answer to this?  I have been getting the same error and it is driving me nuts!
Neha Patil 20Neha Patil 20
Hi Adam,

No i couldnt get this working. So i then created connected app in target org and used the consumer key from the same in source org to fetch the session id. This Named credential feature doesnt work for the approach i mentioned above

Adam Drissel 1Adam Drissel 1
Hey Neha,

I'm trying to acquire the Session Id for use in the Apex Controller for my Lightning app.  I'm not sure the source org/target org situation will apply in my case, unfortunately.  Any ideas on how to get it working in a Lightning component (get the Session Id, that is)?
Neha Patil 20Neha Patil 20
Hi Adam,
In that case why do you need a session id if you dont want to integrate with an another org?

Adam Drissel 1Adam Drissel 1
I need to add a dependent picklist in my Lightning app, which is otherwise impossible, from what I'm seeing.  Unless you know if a simpler way?  

Basically, the information for controlling field values is available when object/field data is pulled via the API.  If I can get that information in JSON, I can use other logic to parse it accordingly, and create lists of items and their corresponding dependencies.

Is there another way to do this that you are aware of? 

Thanks again!
Adam Drissel 1Adam Drissel 1
Hey Neha,

Just as an FYI, I posted a new forum question here:!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G000000UbJkQAK

Hopefully someone gets on it.  From what I'm seeing online, this is a big pain in the *** for a lot of people.  I understand the security measures, but with everything pushing towards Lightning, and the API being such a HUGE thing in Salesforce, how can the two not have some form of integration?!


Neha Patil 20Neha Patil 20
Hi Adam,

Not sure if am getting this correctly bt to fetch the fields of an object you can also use Schema.DescribeFieldResult and then input your further logic. I found this link here

Hope this helps :)

Hi All,
Refer for complete step by step instructions :
Issue : [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
Root Cause : 
1. Not approved for access
2. API security token required
Fix :
-->ConnectedApp | OAuth Policies |Permitted Users -->Select --> All Users may self-authorize
--> ConnectedApp | OAuth Policies | IP Relaxation -->Select --> Relax IP restrictions
--> If your organization security token is enabled,Need to add password+security token in above password field
--> If IP range is enabled and still getting the above error,Please verify on connected app permission is granted or not.Provide the permission for that user.

Tomáš Migra 8Tomáš Migra 8
Check if you didn't use Lightning URL. You should always use Classic url in Named credentials.