2

I have written the code below to call a future method from a scheduled class. The problem is that the session Id is expiring by the time the scheduled class runs and hence the future method is never running. As a workaround I tried explicitly logging in as an user through code before we call the future method but that too is not working as the session Id for the user above is not getting passed properly. Can any one please tell me what i am doing wrong?

public class SQRDailyReport_Schedulable implements Schedulable {
private String sessionId;
private static final String NS_SOAP = 'http://schemas.xmlsoap.org/soap/envelope/';
private static final String NS_SF = 'urn:partner.soap.sforce.com';

public SQRDailyReport_Schedulable () {

sessionId = retrieveSessionId('User', 'PwdSCToken');
//this.sessionId = SessionId;
}

public void execute(SchedulableContext SC) {
SQRCall.sendVF(sessionId);

}
public static string retrieveSessionId(String user, String password) {
  if (!Test.isRunningTest()) {
    HttpRequest req = new HttpRequest();
    req.setMethod('POST');   
    req.setTimeout(60000);
    req.setEndpoint('...');
    req.setHeader('Content-Type', 'text/xml;charset=UTF-8');        
    req.setHeader('SOAPAction', '""');
    req.setBody('....');        
    HttpResponse res =  new Http().send(req);
    if(res.getStatusCode() != 200)
    {
        Dom.Document responseDocument = res.getBodyDocument();
        Dom.Xmlnode rootElm = responseDocument.getRootElement(); // soapenv:Envelope
        Dom.Xmlnode bodyElm = rootElm.getChildElement('Body', NS_SOAP); // soapenv:Body 
        Dom.Xmlnode faultElm = bodyElm.getChildElement('Fault', NS_SOAP); // soapenv:Fault
        Dom.Xmlnode faultStringElm = faultElm.getChildElement('faultstring', null); // faultstring          
        throw new LoginException(faultStringElm.getText()); 
    }
    // As per http://wiki.developerforce.com/page/Enterprise_Login
    Dom.Document responseDocument = res.getBodyDocument();
    Dom.Xmlnode rootElm = responseDocument.getRootElement(); // soapenv:Envelope
    Dom.Xmlnode bodyElm = rootElm.getChildElement('Body', NS_SOAP); // soapenv:Body 
    Dom.Xmlnode loginResponseElm = bodyElm.getChildElement('loginResponse', NS_SF); // loginResponse
    Dom.Xmlnode resultElm = loginResponseElm.getChildElement('result', NS_SF); // result
    Dom.Xmlnode sessionIdElm = resultElm.getChildElement('sessionId', NS_SF); // sessionId
    return sessionIdElm.getText();
  } else {
    String sessionId = '';
    return sessionId;
  }
}
public class LoginException extends Exception {}
}
  • did you look at salesforce.stackexchange.com/q/22704/2602? – cropredy Oct 3 '14 at 0:55
  • Yes. In fact the retrieveSessionId method that I wrote above is similar to the one suggested. However, the session Id that is getting passed is still of the user who schedules the class and not of the user who was logged in with the retrieveSessionId method. – Debarshi Oct 3 '14 at 15:46
  • @AndrewFawcett: Hi Andrew, do you have any insights as to where I might be wrong. Really appreciate your help. – Debarshi Oct 6 '14 at 17:05
1

Here login body must be

<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <n1:login xmlns:n1="urn:partner.soap.sforce.com">
            <n1:username>your_username</n1:username>
            <n1:password>your_password</n1:password>
        </n1:login>
    </env:Body>
</env:Envelope>

And SOAPActionheader should be login. You have set it an empty string.

  • Thanks @regal. I tried it, however the session ID which gets passed in the execute method (SQRCall.sendVF(sessionId);), is still the sesssion Id of the user who schedules the job and not of the user whom I am trying to login with the code. – Debarshi Oct 2 '14 at 22:46

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.