0

I have been trying to make this work however no luck. I have a requirement where I need to modify the static resource on demand via apex. I downloaded MetadataService class from git (https://github.com/certinia/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls).

I have setup JWT connected APP, named creds, external creds correctly. Ran the below snippet and got 200.

request.setEndPoint('callout:MetadataJWT/services/data'); request.setMethod('GET'); HttpResponse response = new HTTP().send(request); System.assertEquals(200, response.getStatusCode()); 

I have below code where I am utilising this metadata api:

public static void updateStaticResource(){ MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.endpoint_x = 'callout:MetadataJWT/services/Soap/m/60.0'; MetadataService.StaticResource staticResource = new MetadataService.StaticResource(); staticResource.fullName = 'TESTHOG'; staticResource.contentType = 'application/json'; staticResource.cacheControl = 'public'; staticResource.content = EncodingUtil.base64Encode(Blob.valueOf('Static 1 stuff Changed')); List<MetadataService.SaveResult> results = service.updateMetadata( new MetadataService.Metadata[] { staticResource }); } 

I am no where setting session id instead using named creds for authenticating. But still gets this error: 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=

Any help is much appreciated.

1 Answer 1

-1

You need to set the session id for the metadataport instance to your named credential. service.SessionHeader.sessionId = '{!$Credential.OAuthToken}';

Also, there's an issue with the way the Metadata API behaves that causes the token refresh process to not occur properly. In a nutshell, everything works fine after setup, but after a period of time (when your session expires), you get this session_expired error.

There is a workaround given in this blog which has worked for me in the past. Also, salesforce also has published an article on it.

1
  • This has been tried as well, exact steps from this blog with no luck Commented Jun 26 at 21:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.