[SalesForce] Invoke Apex Class with Process Builder

I am attempting to start this Apex class with Process Builder, triggered by a field change. I've never done this before and I'm unsure what invoke code needs to be added in order to accomplish this. I tried adding @InvocableMethod but I'm getting an error telling me this :

Compile Error: Unsupported parameter type String. 
Valid invocable parameters must be List types like List<T> 
where T is a supported type at line 26 column 27

It is a class which is currently being invoked with a Lightning Component button in a record.

public class UpdateContactPCRController {

    @InvocableMethod
    public static String  updateContact(String recordId){        
       
        String contactId = '';       
        
        List<Contact> contactToBeupdate = new List<Contact>();        
        
        if(String.isNotBlank(recordId)){            
            
            List<Program_Contact_Role__c> programContacList = [SELECT Id,Contact__c,Program_Name__c FROM Program_Contact_Role__c WHERE Id =:recordId AND Contact__c != null];
            contactId = programContacList[0].Contact__c;
            
            if(String.isNotBlank(contactId)){
                
                contactToBeupdate = [Select Id,Pardot_Action_Trigger__c,PCR_Register_Button_Link__c,PCR_URL_for_UI__c FROM Contact Where Id =: contactId Limit 1];                
                
                contactToBeupdate[0].Program_Contact_Role_Id__c = recordId;
            }
                        List<Program_Communication_Recipients__c> programCommunicationRecs = [Select Id,Name,
                                  Program_Communication__r.Program__r.Buyer__r.Name,Receipient__c,                
                                              From Program_Communication_Recipients__c Where 
                                                           Program_Communication__r.Program__c =: programContacList[0].Program_Name__c AND 
                                                           Receipient__c =: programContacList[0].Id];                                           
                                
                
                    contactToBeupdate[0].PCR_Welcome_Message__c = String.valueOf(programCommunicationRecs[0].Program_Communication__r.Welcome_Message__c);                                                    

        if(contactToBeupdate.size() > 0){
            update contactToBeupdate;  
        }
        
        return 'Updated Successfully';
    }
}
    //Calling  Http_Utility_Pardot class pardotCreateProspect method for the creation of Prospect record.
    String returnedResponseFromPardot = Http_Utility_Pardot.pardotCreateProspect(new Set<Id> {contactToBeupdate[0].Id});

Thank you very much for any help you can give with this. It is very appreciated.

Best Answer

String is not an allowed argument because Invocable Methods must be passed Lists. Below is taken from the Salesforce developer documentation on Invocable Methods. You will need to make your method accept a List<String> and return a List<String> or a another List type.

InvocableMethod Considerations

Implementation Notes

  • The invocable method must be static and public or global, and its class must be an outer class.
  • Only one method in a class can have the InvocableMethod annotation.
  • Other annotations can’t be used with the InvocableMethod annotation.

Inputs and Outputs

  • There can be at most one input parameter and its data type must be one of the following:
  • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
  • A list of an sObject type or a list of lists of an sObject type.
  • A list of the generic sObject type (List) or a list of lists of the generic sObject type (List<List>).
  • A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.

If the return type is not Null, the data type returned by the method must be one of the following:

  • A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
  • A list of an sObject type or a list of lists of an sObject type.
  • A list of the generic sObject type (List) or a list of lists of the generic sObject type (List<List>).
  • A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.