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
Prashant Bhatt 2Prashant Bhatt 2 

Set SessionID in lightning component

HI everyone, 

I am working on salesforce Lightning movement and got stuck while converting a Intacct classic button to lightning button.

Process
We have 'Sync to Intacct' button which calls a intacct URL to sync up SF data to intacct.
we need to pass session Id and partner_server_url in the url.

Issue
Now when I am trying to create a lightning component for this button, I am getting error ' Unable to validate Salesforce.com session is active'.
I have gone thorugh couple of post and found that UserInfo.getSessionId() do not work in lightning component.

So I have tried creating a VF page to get the session id
<apex:page standardStylesheets="false" lightningStylesheets="false">
    <body>
        <span style="display: block">
            SESSION_ID_START{!$Api.Session_ID}SESSION_ID_END
        </span>
    </body>
</apex:page>
I am using this session Id in my page
@AuraEnabled
    webService static String getSessionIdFromVFPage() {
            
            //PageReference page = new PageReference('apex/SessionId');
            PageReference PageSessionId = Page.SessionId;
            String pageContent = PageSessionId.getContent().toString();
         System.debug(pageContent);
            String SESSION_ID_START = 'SESSION_ID_START';
            String SESSION_ID_END = 'SESSION_ID_END';
            
            Integer startIndex = pageContent.indexOf(SESSION_ID_START) + SESSION_ID_START.length();
            Integer endIndex = pageContent.indexOf(SESSION_ID_END);
        System.debug(endIndex);
            //System.assert( false, PageSessionId.getContent().toString() );
            //System.assert( false, pageContent.substring(startIndex) );
            //System.assert( false, startIndex + ':' + endIndex );
            String sessionId=pageContent.substring(startIndex, endIndex);
            System.debug(sessionId);
            return sessionId;
}

Now when I am calling this function in my component I am getting error index out of bound -1  -cmp.get("c.getSessionIdFromVFPage");

Also, When I am trying to use the same controller function using anonymous block I get the desired result.

I have followed below post-
https://salesforce.stackexchange.com/questions/110515/getting-session-id-in-lightning

Thanks in advance.
 
Raj VakatiRaj Vakati
I think you can pass UserInfo.getSessionId() out to lightning component via an @AuraEnabled an use it for API calls.
Raj VakatiRaj Vakati
Refer this links 

https://sfdcpanther.wordpress.com/2018/02/05/how-to-call-salesforce-api-from-lightning-component/
https://ipfs-sec.stackexchange.cloudflare-ipfs.com/salesforce/A/question/110515.html
http://eltoro.force.com/apex/ArticlePreview?id=a07A000000dq3miIAA