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
Venkatesh SVenkatesh S 

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

Hello All,

I am trying to integrate two instance via metadata api.  I have used oauth to get access token and passed it to metadata api session.  When i called the method then this error came out.  Can anyone please suggest some workaround to use another salesforce instance session in seperate salesforce instance metadata api codings.
 
MetadataService.MetadataPort service1 = new MetadataService.MetadataPort();
        service1.SessionHeader = new MetadataService.SessionHeader_element();
        service1.SessionHeader.sessionId = access_token;

Thanks.
Daniel BallingerDaniel Ballinger
You will need to adjust the endpoint for the pod/instance where the Salesforce session is valid. It will typically be in the instance_url that comes back with the access_token.

It looks like you are using Apex to call the Metadata API. In that case it will be something like:
 
service1.endpoint_x = instance_url;

 
Venkatesh SVenkatesh S
Hi Daniel, 
Thanks for your response,
 
MetadataService.MetadataPort service1 = new MetadataService.MetadataPort();
        service1.SessionHeader = new MetadataService.SessionHeader_element();
        service1.SessionHeader.sessionId = Session;
        service1.endpoint_x = 'https://xyz.my.salesforce.com';

I have hardcoded the instance url that came from the session id response. It works, but there is another issue

Web service callout failed: Unexpected element. Parser was expecting element 'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found 'http://www.w3.org/1999/xhtml:html'

Thanks.

 
Daniel BallingerDaniel Ballinger
What tool did you use to generate the MetadataService Apex class? Are you using Wsdl2Apex?
What method are you calling on the MetadataAPI to get that error?

Also, avoid hardcoding the endpoint_x if you can. The instance_url can change with things like pod migrations.
Venkatesh SVenkatesh S
I am using this code which i have collected from github,
global class deployClass {
    webservice Static void deploy(String zip,String Session){
        
        MetadataService.MetadataPort service1 = new MetadataService.MetadataPort();
        service1.SessionHeader = new MetadataService.SessionHeader_element();
        service1.SessionHeader.sessionId = Session;
        service1.endpoint_x = 'https://xyz.my.salesforce.com';

        
            MetadataService.MetadataPort service = service1;
            MetadataService.DeployOptions deployOptions = new MetadataService.DeployOptions();
            deployOptions.allowMissingFiles = false;
            deployOptions.autoUpdatePackage = false;
            deployOptions.checkOnly = false;
            deployOptions.ignoreWarnings = false;
            deployOptions.performRetrieve = false;
            deployOptions.purgeOnDelete = false;
            deployOptions.rollbackOnError = true;
            deployOptions.testLevel = 'NoTestRun';
            deployOptions.singlePackage = true;		
            MetadataService.AsyncResult AsyncResult = service.deploy(zip, DeployOptions);
    }

}

This code is working for the instance which i currently logged in instance session, but if i use another instance only those errors are coming.
LBSLBS
Hi Venkatesh,

Were you able to find a workaround for this?