25

I am building sfdc application that required me to call and do add/update using Http method at vendor application in external server daily. They created one account for me in their application. In order to proceed with data manipulation at their application, I need to retrieve the token by passing my session id using UserInfo.getSessionId().

After retrieved the token, then I am able to do other process.The issue is, when I run my code in Apex Execute I am able to retrieve UserInfo.getSessionId(), but when I put in Schedule and run it ,the session Id return null.

global class Test_Sched  implements Schedulable{
global void execute(SchedulableContext sc) {

    System.debug('Test ' + UserInfo.getSessionId());  //This returning null

    String sessionId=UserInfo.getSessionId();
   //pass the session id to the class that call http and process the data in external apps
staticclassprocess(sessionId)   ;      
}
}

I read the documentation here When do Sessions expire? but still not able to understand how to pass the sessionId from scheduler.

  • Take a look at this post salesforce.stackexchange.com/questions/9261/…. (Maybe you have already checked it out) – Prady Nov 29 '13 at 7:22
  • cant your vendor use OAuth, so that you provide them with an access and a refresh token – Seb Wagner Nov 29 '13 at 7:59
  • From @jenneking: Just a note on schedule with Dev Console. // Did not work, did not like where ? was System.schedule('Test_Sched', '0 1-23 * * ?', new Test_Sched(UserInfo.getSessionId())); //This worked System.schedule('Test_Sched', '0 0 1-23 * * ?', new Test_Sched(UserInfo.getSessionId())); – Chris Duncombe Nov 18 '14 at 17:23
21

Pass it in as the initial state, this will mean you have to schedule it in code though. So, your class:

global class Test_Sched  implements Schedulable{
    private String sessionId;

    global Test_Sched( String sessionId ) {
        this.sessionId = sessionId;
    }

    global void execute(SchedulableContext sc) {

        //pass the session id to the class that call http and process the data in external apps
        staticclassprocess(sessionId)   ;      
    }
}

And to schedule it:

System.schedule('Test_Sched', '0 0 1-23 * * ?', new Test_Sched(UserInfo.getSessionId()));

Take heed of the advice/warnings in the post When do Sessions expire?.

I think this works because when you schedule a job, it is serialized with its initial state and then deserialized for use at the scheduled point. Beware of your sessions expiring too (we have found that if you close your browser, its fine but if you 'logoff' from SF then your session will expire immediately).

  • Where do I put System.schedule('Test_Sched', '0 1-23 * * ?', new Test_Sched(UserInfo.getSessionId())); – unidha Nov 29 '13 at 8:48
  • You can put that in Dev Console, then it will appear in your scheduled jobs and run every hour, on the hour – Phil Hawthorn Nov 29 '13 at 8:57
  • You mean once we close the developer console then it won't run anymore?I have to login and run it manually in developer console daily?I don't want to do that way.I thought that we able to get the session id for the person who create the schedule job. – unidha Nov 29 '13 at 9:06
  • No, you run it once and then it is scheduled forever until you cancel it. – Phil Hawthorn Nov 29 '13 at 9:09
  • Thanks @Phil Hawthorn.It works, but I will put the risk of session expired in consideration.Thanks all :) – unidha Dec 3 '13 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.