+ Start a Discussion
Mamadou Diallo 14Mamadou Diallo 14 

WebService returned a SOAP Fault: INVALID_SESSION_ID

Hello there. I have an issue with this trigger and I need help please. I have a custom object called obejctA and a custom picklist field Test in the opportunity object. Here is I want: if a new record X is added to the objectA, I want that record X to be added to the picklist Test. Unfortunatly, when I create a new record on objectA, I got this error "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=_
"

Here is my code (trigger and class):
//////////// THE TRIGGER //////////

trigger UpdateTestFromObjectA on objectA__c (after insert) {
    objectA__c[] xyz = trigger.new;
    UpdateTestPicklist.updatePicklist(Trigger.newMap.keySet()); 
}

//////////// THE CLASS ////////////

public class UpdateTestPicklist {
    public static MetadataService.MetadataPort createService() {
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;
    }
    public class MetadataServiceExamplesException extends Exception { }
    public static void handleSaveResults(MetadataService.SaveResult saveResult)
    {
        // Nothing to see?
        if(saveResult==null || saveResult.success)
            return;
        // Construct error message and throw an exception
        if(saveResult.errors!=null)
        {
            List<String> messages = new List<String>();
            messages.add(
                (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') +
                    'occured processing component ' + saveResult.fullName + '.');
            for(MetadataService.Error error : saveResult.errors)
                messages.add(
                    error.message + ' (' + error.statusCode + ').' +
                    ( error.fields!=null && error.fields.size()>0 ?
                        ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
            if(messages.size()>0)
                throw new MetadataServiceExamplesException(String.join(messages, ' '));
        }
        if(!saveResult.success)
            throw new MetadataServiceExamplesException('Request failed with no specified error.');
    }
    
    public class soapSforceComSchemasClassUpdateOpportunity {
        soapSforceComSchemasClassUpdateOpportunity.SessionHeader_element webserviceSessionHeader = new soapSforceComSchemasClassUpdateOpportunity.SessionHeader_element(); 
        webserviceSessionHeader.sessionId = partnerLoginResult.sessionId; 
        soapSforceComSchemasClassUpdateOpportunity.updatePicklist  myWebservice = new soapSforceComSchemasUpdateOpportunity.updatePicklist();
        myWebservice.SessionHeader = webserviceSessionHeader; 
        myWebservice.updatePicklist('myusername','mypassword')
    }
    @future(callout=true)
    public static void updatePicklist(set<ID> recordIDs)
    {
              
        // Get new record from objectA 
        list<objectA__c> xyz = [SELECT Id, Name FROM objectA__c WHERE Id IN: recordIds];
        for (objectA__c a: xyz){
        
             MetadataService.MetadataPort service = createService();

        // Read Custom Field
        MetadataService.CustomField customField =
            (MetadataService.CustomField) service.readMetadata('CustomField',
                new String[] { 'Opportunity.test__c' }).getRecords()[0];
            
        // Add objectA to picklist values
        metadataservice.PicklistValue theValue = new metadataservice.PicklistValue();
        theValue.fullName= a.Name;
        theValue.default_x=false;  
        customField.picklist.picklistValues.add(theValue);
        
        // Update Custom Field
        handleSaveResults(
            service.updateMetadata(
                new MetadataService.Metadata[] { customField })[0]); 
        }    
    }
}
The SOAP apex classes (AsyncSoapSforceCom200608Apex, soapSforceCom200608Apex etc.. )exist in my org. 
Thank you again for your help.