I am using enterprise wsdl to login and get session id. as I have verified in login history, Its logged in successfully. But it throws the error when it invokes the another sfdc webservice from future call.

caused by: System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session key: 00DS00000009UqA!AQIAQKOw6N5keVHqAYpYBcmUV_8jm_M0o2r.zUuE1AuH3hiirb4jG65iyOewJNwEIAkCwRALXeu5ppoz07bVuY.XXXXXX
This is expected, it can happen if the session has expired and swept away, or if the user logs out, or if its just someone trying to hack in.  faultcode=sf:INVALID_SESSION_ID faultactor=

I have made the end point domain unique.what could be the cause of this issue?

My Code:

    public static void addPDFAttachment(List<Id> lstId){

        enterpriseSoapSforceCom.LoginResult lr = new enterpriseSoapSforceCom.LoginResult();
        enterpriseSoapSforceCom.Soap sp = new enterpriseSoapSforceCom.Soap();

        sp.timeout_x = 60000;

        lr = sp.login(username, password); 

        SoapSforceAttachAPI.AttachPDFforArchiving objAttachPDFInstance = new SoapSforceAttachAPI.AttachPDFforArchiving();
        SoapSforceAttachAPI.SessionHeader_element sessionHeader = new SoapSforceAttachAPI.SessionHeader_element(); 
        sessionHeader.sessionId = lr.sessionId;
        objAttachPDFInstance.SessionHeader = new SoapSforceAttachAPI.SessionHeader_element();
        objAttachPDFInstance.SessionHeader.sessionId = lr.sessionId;

        objAttachPDFInstance.attachPDF(lstId, '', 'No'); 
  • Are you trying to consume a Salesforce SOAP API from other Org by using Apex ?? Could you paste your code? – Martin Borthiry Mar 4 '14 at 8:21
  • I am using webservice from same org. I have create apex webservice to create PDF. – gold Mar 4 '14 at 9:42
  • What is the value of sessionId, are you setting it from the LoginResult? – techtrekker Mar 4 '14 at 11:57
  • yes I am getting it from LoginResult. – gold Mar 5 '14 at 4:34
  • 1
    Why can't you call the attachPDF method directly? Since WS are static classes you could do `MyWebServiceClass.attachPDF(lstId,'','No'); – Martin Borthiry Mar 5 '14 at 9:27

For Apex code that is executed asynchronously, such as @future methods, Batch Apex jobs, or scheduled Apex jobs, getSessionId returns null.


Salesforce(SF) has used load sharing by allocating a pool of clients to an instance. For ex. if you look at the URL once you login to your SF account, it would be www.na19....

Periodically SF keeps migrating their clients from one instance to other. It's worth checking if your instance has been moved and if you are using the old wsdl that is still pointing to the old instance SF it will throw this error.

Your Answer

