I am using MetadataService from LWC and since I don't get the session id in Lightning context, I am doing preauthorizing the org and getting the access token and storing it in a custom object. However, if I try to use the Access token to create metadata service, I am getting the error that Invalid Session id, however, the access token is a valid one not expired.

ConnectedOrg__c orgDetail = getOrgDetails(org);
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
//service.SessionHeader.sessionId = UserInfo.getSessionId();
service.SessionHeader.sessionId = orgDetail.Access_Token__c;
service.timeout_x = 40000;


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 hash: rRUFiKhfdvN/6alywRuuc3IY/nMF6Z7ADC2H4RA= This error usually occurs after a session expires or a user logs out. Decoder: DataInDbSessionKeyDecoder faultcode=sf:INVALID_SESSION_ID faultactor=

1 Answer 1


I forgot to add the endpoint of other org.

service.endpoint_x = orgDetail.Instance_URL__c+ '/services/Soap/m/'+METADATA_API_VERSION;

It is working fine now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .