How can I get a valid Session ID in Lightning?

Lightning Message Channels

Question

When working with the Apex Wrapper Salesforce Metadata API, developers often need a valid API-capable session ID to perform operations such as updating picklist values dynamically. In Salesforce Classic, it is possible to retrieve a valid session ID using UserInfo.getSessionId() and pass it into an asynchronous method for Metadata API callouts.

However, in Salesforce Lightning, developers run into an INVALID_SESSION_ID error. This happens even though System.debug(UserInfo.getSessionId()) prints what looks like a valid session ID in both Classic and Lightning. The issue arises because for asynchronous Apex code, such as @future methods, batch jobs, or scheduled jobs, the session ID is not valid for API use when executed in the Lightning runtime.

Answer

Salesforce does not currently provide a supported way to get an API-capable session ID from Lightning Components. Unlike in Classic, Lightning does not expose the $Api.Session_ID global variable, and this behavior is intentional. By design, Lightning prevents direct access to an API session ID due to security reasons.

From Salesforce’s partner discussions:

  1. There is no $Api global variable in Lightning Components, so {!$Api.Session_ID} cannot be used.
  2. This restriction is intentional, and there is no official way to bypass it.
  3. The only workaround is to use a hack, but it is not recommended because of poor performance and security implications.

Workaround

If you absolutely need a valid API session ID in Lightning, you can build a workaround using a Visualforce page and Apex. The Visualforce page can still access $Api.Session_ID. You can then retrieve that value in Apex using the getContent() method.

Example Visualforce page:

<apex:page>
    Start_Of_Session_Id{!$Api.Session_ID}End_Of_Session_Id
</apex:page>

Helper Apex class to extract the session ID:

global class Utils {
    global static String getSessionIdFromVFPage(PageReference visualforcePage) {
        String content = visualforcePage.getContent().toString();
        Integer s = content.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(),
                e = content.indexOf('End_Of_Session_Id');
        return content.substring(s, e);
    }
}

Using it in the Metadata API service:

public static MetadataService.MetadataPort createService() {
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = Utils.getSessionIdFromVFPage(Page.SessionId);
    return service;
}

This way, you obtain a valid session ID that works with the Metadata API in Lightning. But again, Salesforce does not recommend this approach because it is a hack, may degrade performance, and bypasses intentional design restrictions.

Conclusion

In Classic, using UserInfo.getSessionId() works fine. In Lightning, there is no supported way to obtain an API-capable session ID due to security restrictions. The Visualforce getContent() trick is the only workaround, but it should be used carefully and only when absolutely necessary.

Enroll for Salesforce Training Designed for Career Building Success

Our Salesforce course is meticulously designed to provide a thorough understanding of the Salesforce platform, equipping you with the essential skills to excel in the CRM industry. The curriculum includes key modules such as Salesforce Admin, Developer, and AI, blending theoretical knowledge with hands-on experience. Through practical projects and real-world exercises, you’ll gain the expertise needed to tackle complex business challenges using Salesforce solutions. Our expert instructors ensure you acquire both technical proficiency and industry insights to succeed in the Salesforce ecosystem.

In addition to technical training, our Salesforce training in Bangalore offers personalized mentorship, certification guidance, and interview preparation to enhance your career prospects. You’ll have access to extensive study materials, live project experience, and continuous support throughout your learning journey. By the end of the course, you’ll be fully prepared for certification exams and equipped with the problem-solving skills that employers value. Start your Salesforce career with us—enroll in a Free Demo today!

0
Would love your thoughts, please comment.x
()
x