Last active
October 16, 2019 23:14
-
-
Save ChuckJonas/36c9eb0d204db02c2c8a3057195a8e50 to your computer and use it in GitHub Desktop.
Invokable as Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modeled after example found here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm | |
// Expanded to show how an interface might help overcome some of the limits around existing @Invokable implementation | |
public class AccountInsertInvokable implements Invokable<Account, Id> { | |
private Boolean allOrNone; | |
private Boolean logFailures; | |
// @InvokableSetup this as a configuration point for ProcessBuilder/Flow | |
// Params Restricted to same types as @InvocableVariable | |
@InvokableSetup | |
public AccountInsertInvokable(Boolean allOrNone) { | |
this(allOrNone, false); // assume we cannot run "logger" during invokable context for some reason | |
} | |
//Alternate constructor which can be called only via Apex | |
public AccountInsertInvokable(Boolean allOrNone, Boolean logFailures) { | |
this.allOrNone = allOrNone; | |
this.logFailures = logFailures; | |
} | |
public Id[] Invoke(Account[] input){ | |
Database.SaveResult[] results = Database.insert(accounts, this.allOrNone); | |
List<ID> accountIds = new List<ID>(); | |
for (Database.SaveResult result : results) { | |
// bug because return.size won't always == input.size. Took this example from the offical doc tho... | |
if (result.isSuccess()) { | |
accountIds.add(result.getId()); | |
}else if(this.logFailures){ | |
Logger.logDMLFailure(result); | |
} | |
} | |
return accountIds; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I & R have same type restrictions are existing invokable | |
public interface Invokable<I, R> { | |
R[] Invoke(I[] input); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment