Tuesday, April 5, 2016

Use Apex @InvocableMethod with Process Builder

It's one great way to marry the versatility of Apex to implement complex logic and the easy administration of Processes.  Say you want to auto-unfollow old Contracts a period of time after they're terminated for those who manage a lot of contracts.  It's pretty straightforward to do the unfollowing in Apex, then use Process to invoke the action and leave it to the administrators to control how long the wait period should be.  Bob Buzzard has a good piece on the general pattern, and info about @InvocableMethod can be found here.

The only thing I found a bit non-intuitive is how parameter is passed to the Invocable Method, which only takes List of arguments.  Process Builder, on the other hand, doesn't have an obvious way to create an array/list variable.  It turns out that all you need is to set a single value of the correct type (of the List element).  For instance if the method expects a list of IDs, just set a single ID, which is called reference in Process Builder.

"scids" is the argument expected by the Apex method.

    @InvocableMethod(label='Chatter unsub for Service Contracts' description='Unfollow terminated/cancelled service contracts for PMM team')
    public static List UnfollowSCs(List scIds) {

No comments:

Post a Comment