Last active
August 9, 2018 12:20
-
-
Save Sunil02kumar/11a7739df32edf7882afb2e47340bb40 to your computer and use it in GitHub Desktop.
Finding Session id for User in Future Method or Batch Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page showHeader="true" sidebar="true"> | |
SessionIDStart{!$Api.Session_ID}SessionIDEnd | |
StartVFPageBaseURL{!LEFT($CurrentPage.URL,FIND('/',$CurrentPage.URL,9))}EndVFPageBaseURL | |
</apex:page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Author- sunil Kumar | |
//Twitter handle-@sunil02kumar | |
//purpose- finding user sessionid in asynchrounous apex call | |
public class SessionIdUtility{ | |
@future(callout=true) | |
public static void checkIfUserSessionIdExistInFutureCall(){ | |
HttpRequest req = new HttpRequest(); | |
system.debug('********session Id using UserInfo.getSessionID():'+UserInfo.getSessionID()); | |
string sessionId=findSessionIdUsingVFPage(Page.APICallUtilityVF); | |
system.debug('********session Id by generating from VF:'+sessionId); | |
} | |
public static String findSessionIdUsingVFPage(PageReference visualforcePage){ | |
String content = visualforcePage.getContent().toString(); | |
Integer startIndex = content.indexOf('SessionIDStart') + 'SessionIDStart'.length(), | |
endIndex = content.indexOf('SessionIDEnd'); | |
return content.substring(startIndex, endIndex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment