Salesforce
Due to security reasons salesforce does not expose session id directly .hence sometimes this became head ache for the developer to fetch current logged in user session id by UserInfo.getSessionID() . Today in this post i will let you know how to fetch the User Session id by the help of visualforce page below is the code snippets. VisualForce Page : GetSessionId
<apex:page contentType="application/json">{"sessionId":"{!$Api.Session_ID}"}</apex:page>
Now From your apex class deserialize your vf page to get session id Apex Class : GetSessionId
public class GetSessionId {
    public class SessionId {
        public string sessionId;
    }
    
    @AuraEnabled
    public static string getUserSessionId() {
         SessionId sessionJson = new SessionId();
        if(!Test.isRunningTest()){
             sessionJson = (SessionId)JSON.deserialize(Page.YourVFPage.getContent().toString(), SessionId.class);
        }
        return sessionJson.sessionId;
    }

Hits: 464

Share Post

By Himanshu Rana

My Name is Himanshu Rana, 23 Years young, born and grow up in Ghaziabad, India. A High Spirited Salesforce Admin, Developer and a Blogger. I currently work at Wakencode Technologies,

Leave a Reply

Your email address will not be published. Required fields are marked *