0

I have a method in my code that I want to make into an invocable method:

// This should be an invocable method with @InvocableMethod
private void updateContacts(Map<Id,Contact> newContactMap, Map<Id,Contact> oldContactMap){
    
    for (Contact newContact : newContactMap.values())
    {
        // At this point, I absolutely need the new and old map for another class.
        if(newContact.Name == 'Example Logic'){
            OtherClass.ProcessStuff(newContactMap, oldContactMap);
        }
       
    }

}

And I call this method in a trigger handler like so:

public void onAfterUpdate(Map<Id,Contact> newContactMap, Map<Id,Contact> oldContactMap) {   
        updateContacts(newContactMap, oldContactMap);
}

How can I pass in the maps to the invocable method without changing the updateContacts method? (Changing the method parameters are fine, but ultimately it needs to work on maps later on)

1 Answer 1

3

You could:

  • define a wrapper class with two contact list. This is needed to overcome the invocable method limit about only one parameter.
  • define a new invocable method that receives a list of wrapper class instead of relying on the existing one
  • create the two required maps leveraging the appropriate constructor
  • call the existing updateContacts method

Wrapper:

public class ContactListDTO {
   @InvocableVariable(required=true)
   public List<Contact> newContactList;

   @InvocableVariable(required=true)
   public List<Contact> oldContactList;
}

Invocable method:

@InvocableMethod
public static void updateContact(List<ContactListDTO> inputContacts) {
    for (ContactListDTO dto : inputContacts) {
        Map<Id,Contact> newContactMap = new Map<Id,Contact>(dto.newContactList);
        Map<Id,Contact> oldContactMap = new Map<Id,Contact>(dto.oldContactList);
        updateContact(newContactMap, oldContactMap);
    }
}

Methods within a class can have the same name if they have different parameter lists because Apex supports overloading methods and can distinguish between methods with different method signatures.

3
  • Hm, except Only one parameter is supported on methods with @Invocable annotation
    – Josiah
    Mar 14, 2022 at 14:16
  • @Josiah right! I updated the answer.
    – RubenDG
    Mar 14, 2022 at 17:02
  • Thank you Ruben.
    – Josiah
    Mar 17, 2022 at 22:40

You must log in to answer this question.

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