Invocable Methods and the Apex Data Source Action

By annotating your Apex code as an InvocableMethod with InvocableVariables, you’ll be able to directly utilize your Apex within Skuid using the Action Framework—without any additional JavaScript. This approach works well if your Apex logic has a general purpose, such as “Send SMS via Twilio,” and thus should be available to call from any Skuid Page.

To accomplish this:

  1. Annotate your Apex method as an @InvocableMethod
  2. Access your Apex method in Skuid using the Apex Salesforce data source action.

While this method is preferable due to its ease of use within Skuid, there are some considerations you’ll need to keep in mind, listed at the bottom of Salesforce’s InvocableMethod annotation documentation page. One important caveat is that, while you can lock down access to Invocable Apex Classes via Profile / Permission Sets, you cannot do so by execution context (e.g. VF Page). If you need to lock down access to this Apex from a particular execution context, then the Remote Action approach would be better.

Note

  • Each time a Salesforce data source action is run within a Skuid page, an API call is consumed. If Salesforce API limits are a concern for your org, this may not be the best method for accessing Apex. For more information see the Salesforce API Usage section.
  • Salesforce documentation notes that InvocableMethods must receive a List as its parameter. The List your InvocableMethod receives is a class that contains each of your InvocableVariables. Set up a class for the List type to pass into the method, and then for every @InvocableVariable defined in the class, you will be able to pass in a value from Skuid.
  • Only authenticated users can access InvocableMethods. If you have a Force.com site or community that allows guest users, these non-authenticated users will not be able to use any Action Framework sequences that call the Apex Salesforce data source action.

Annotate your Apex as an @InvocableMethod

Annotating your Apex appropriately may be the most challenging aspect of this workflow. Studying the linked documentation above is recommended, in addition to Salesforce’s more general annotation documentation.

Here’s an example of an invocable method that runs an approval process action on a record (in this instance, a Case record) and can be called from Skuid. The ApproveRequest class contains all of the invocable variables, all of which will be available to edit within the Skuid properties pane.

global class CaseApprovalActions {
    @InvocableMethod(label='Approve Record')
    global static void approveRecord(List<ApproveRequest> requests) {
        ApproveRequest req = requests[0];
        ProcessInstanceWorkItem item = [SELECT ID FROM ProcessInstanceWorkItem WHERE ID = :req.workItemId LIMIT 1];
        if (item.Id != null && (req.actionType == 'approve' || req.actionType == 'reject')) {
            Approval.ProcessWorkItemRequest pwr = new Approval.ProcessWorkItemRequest();
            pwr.setWorkItemId(item.Id);
            if (req.actionType == 'approve') { pwr.setAction('Approve'); }
            else if (req.actionType == 'reject') {pwr.setAction('Reject'); }
            pwr.setComments(req.comments);
            Approval.process(pwr);
        }
    }
    global class ApproveRequest {
        @InvocableVariable(required=true description='The pending Work Item ID to approve/reject' label='Work Item Id')
        public ID workItemId;
        @InvocableVariable(required=true description='Comments on why the user is approving/rejecting the record' label='Comments')
        public String comments;
        @InvocableVariable(required=true description='Options are "approve", "reject"' label='Action Type')
        public String actionType;
    }
}

Access the Apex in Skuid using the Apex Salesforce data source action

Once you’ve registered your Apex method, you can call the invocable method using the Action Framework:

  1. Add a button to your page or a global action to your table.
  2. Create a Run Multiple Actions action
  3. Click the Actions tab in the properties pane
  4. Add a Run data source action action type.
  5. Select salesforce as the data source.
  6. For action, select Apex.
  7. Select an annotated method in the Apex action field.

All properly-annotated invocable methods will appear within the Apex Action field picklist; the first method that Skuid detects will likely be selected automatically. If you do not see your method, try saving your page and refreshing the Composer.

After selecting a method, each of the method’s invocable variables will also appear as additional fields. You can pass in various field values from Skuid models to these variables with the use of merge syntax. The invocable method listed in the above example points to the first row in the CurrentWorkitem model.

UA97SkuidApex-0

To debug any issues with the Apex action, add an on-error action to display any received error message.

  1. Click fa fa-exclamation-circle Add on-error Action.
  2. Click the Show message and block UI action type that is created by default.
  3. For value, enter {{$CurrentAction.error.message}}.
  4. Click Save.