function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ravi_PanchalRavi_Panchal 

System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader

Hi,

I am trying to consume Metadata API of one salesforce org to another org and trying to create custom field.

When i execute below code from developer console, i receive error saying : "System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor".

Code :
*****************************************************************************************************
MetadataService.MetadataPort service = createService();   
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'Contact.TestField__c';
    customField.label = 'Test Field';
    customField.type_x = 'Text';
    customField.length = 42;
    MetadataService.AsyncResult[] results = service.create(
    new List<MetadataService.Metadata> { customField });

public static MetadataService.MetadataPort createService()
{
    partnerSoapSforceCom.Soap sp = new partnerSoapSforceCom.Soap();

    partnerSoapSforceCom.LoginResult loginResult = sp.login('username','password');

    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = loginResult.sessionId;
    System.debug(service.SessionHeader.sessionId);
    return service;    
}

*********************************************************************************
PrasanntaPrasannta (Salesforce Developers) 
Hi,

This error message can be thrown in the following scenarios:

1) The affected integration is sharing credentials with a user or another integration. In this case, if the user or the other integration log out of Salesforce.com while the first integration is running, the existing Session ID will be invalidated, and the integration will get the INVALID_SESSION_ID error message the next time it tries to send a message to the Salesforce API.

To solve this problem, avoid sharing credentials and make sure each integration application or user use their own username.

2) One integration is making concurrent calls and not handling the session ID status. Integrations that make concurrent API calls and issue logout calls run a higher risk of receiving INVALID_SESSION_ID errors. For example, if the integration performs the following operations, in this order:

Login
Create
Logout
An operation may attempt to issue a create call even though the other operation has logged out, resulting in an INVALID_SESSION_ID.

Concurrency is not explicitly supported in the Web Services API and the scenario described above is not recommended. However, in some cases, multiple API requests may issue concurrent calls under the same username (i.e. possibly using the same session).  For example, one user may be using the Outlook Edition plugin and a third-party integration at the same time.

Hope this information helps
PrasanntaPrasannta (Salesforce Developers) 
Hi,

Moreover, Any API calls should handle the INVALID_SESSION_ID gracefully by implementing exception handling.  There are common patterns for handling this problem:

Bubbling the error up to the user, prompting for the username and password, and issuing another login call.
Implement a session Id pooling mechanism, whereby the integration is checking the pool of session Ids, rather than issuing login() or logout() calls on a regular basis.
Issue a "ping", such as getServerTimestamp(), to check if the session Id is valid.  If it is valid, proceed with the call.  If it is not valid, attempt to retry the login automatically.
In all cases, extreme caution should be used with regard to storing usernames, passwords, and session Ids.  Consult with a security professional if the level of security in the implementation is in question.
Ravi_PanchalRavi_Panchal
Hi Prasannata,

Thank you for your response. But here the situation is different, i am not able to execute above come successfully. when i execute above code from developer console, i get an error saying Invalid_session_id. But i am getting session id by means of partner wsdl from createservice method.

I receive an error on 'MetadataService.AsyncResult[] results = service.create(
    new List<MetadataService.Metadata> { customField });' statement. Can you please help?
manjunath kademanimanjunath kademani
If you are trying to query for User session  , using  userinfo.getsession  in FUTURE method,  you will get this error. Try to pass session to future method as parameter , to avoid this type of error
SalesforceCodeLearnerSalesforceCodeLearner
@Ravi_kanchan, By any chance have you resolved this issue If yes please post the solution here. Thanks in advance