Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Last active August 9, 2018 12:20
Show Gist options
  • Save Sunil02kumar/11a7739df32edf7882afb2e47340bb40 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/11a7739df32edf7882afb2e47340bb40 to your computer and use it in GitHub Desktop.
Finding Session id for User in Future Method or Batch Class
<apex:page showHeader="true" sidebar="true">
SessionIDStart{!$Api.Session_ID}SessionIDEnd
StartVFPageBaseURL{!LEFT($CurrentPage.URL,FIND('/',$CurrentPage.URL,9))}EndVFPageBaseURL
</apex:page>
//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