[SalesForce] metadata api : INVALID_SESSION_ID:

To update a picklist value I'm referring to the metadataclass.cls

I'm calling a class method (as like in metadadataclass example) to update the picklist value via a trigger (class method set to @Future(callout=true)) which seems to work but in the log i get an error as below:

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=

Please any help on to fix this error so that i would be able to update the picklist value for the picklist list (custom field)?

Best Answer

UserInfo.getSessionId() returns null in asynchronous contexts so you have to pass the session ID in when you call the future method:

public class PicklistUpdater {

    public static void updateInFuture() {
        updateInFuture(UserInfo.getSessionId());
    }

    @future(callout=true)
    private static void updateInFuture(String sessionId) {
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = sessionId;
        service ...
    }
}

Not sure how high the risk is of the session ID no longer being valid by the time the future executes.

Related Topic