The session id can be fetch through UserInfo.getSessionId() method in apex but which is not available lightning context. Named credentials and connected apps can be used to connect with external web services and internal salesforce api’s with Salesforce. This would requires more configurational effort and changes should be align with each deployments with respect to the orgs.
Here, I would like to share you a work around to solve this issue by using a custom visual force page and an apex controller.
<apex:page controller="LexSessionController"> | |
Start{!$Api.Session_ID}End | |
</apex:page> |
public class LexSessionController { | |
public static String fetchUserSessionId(){ | |
String sessionId = ''; | |
// Refer to the Page | |
PageReference sessionIdPage = Page.LexGetSessionIdPage; | |
// Get the content of the VF page | |
String vfContent = sessionIdPage.getContent().toString(); | |
// Find the position of Start and End | |
Integer startPosition = vfContent.indexOf('Start') + 'Start'.length(), | |
endPosition = vfContent.indexOf('End'); | |
// Get the Session Id | |
sessionId = vfContent.substring(startPosition, endPosition); | |
System.debug('sessionId '+sessionId); | |
// Return Session Id | |
return sessionId; | |
} | |
} |
String sessionId = LexSessionController.fetchUserSessionId();
This expression {!$Api.Session_ID} provides the session id of the current user in the VF page, which is extracted in the apex controller. the below code snippet helps to get the session id in the lightning context (AuraEnabled methods).
'개발자정보' 카테고리의 다른 글
Configure Single Sign On Across Multiple Salesforce Orgs (0) | 2022.04.16 |
---|---|
로컬 컴퓨터에서 LWC 개발 (0) | 2022.04.16 |
나의 첫번쨰 Lightning Web Component (0) | 2022.04.16 |
Postman를 이용한 Salesforce Apex REST APIs 사용 방법 (0) | 2022.04.16 |
Deploy & Retrieve Metadata Using Salesforce DX And VS Code (0) | 2022.04.16 |