Friday, November 8, 2019

How to Get the Session Id of Current User in Lightning Development



You may be faced with Error is "errorCode": "INVALID_SESSION_ID"  while doing the external web services and internal salesforce API Callouts with Salesforce in lighting development.

The session id can be fetched through UserInfo.getSessionId() method in apex but which is not available lightning context.
you can use the below solution it will resolve the error.

Visualforce Page
<apex:page contentType="application/json">
    {!$Api.Session_ID}
</apex:page>

Apex Class
public class SessionIdController {
 public static String getSessionId() {
        return Page.GenerateSessionIdForLWC.getContent().toString();
    }
}

simply use the Apex method where you need to the Session-Id.

No comments:

Post a Comment