Skip to content

Instantly share code, notes, and snippets.

@ChuckJonas
Last active October 16, 2019 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChuckJonas/36c9eb0d204db02c2c8a3057195a8e50 to your computer and use it in GitHub Desktop.
Save ChuckJonas/36c9eb0d204db02c2c8a3057195a8e50 to your computer and use it in GitHub Desktop.
Invokable as Interface
// 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;
}
}
// 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