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 {}
}