Is it possible to call an invocable method from a snippet?

  • 1
  • Question
  • Updated 2 years ago
  • In Progress
I would like to be able to call an invocable method(apex) from a snippet. I realize that you can use the custom action but my requirement is to call the method on load to check a value without the user taking any action.
Photo of Jaime

Jaime

  • 1,220 Points 1k badge 2x thumb

Posted 2 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,004 Points 20k badge 2x thumb
Jaime, the easiest way to do this would be to invoke the Action Framework from Javascript. Use the Page Composer to create the Custom Apex Action as an Action, e.g. as one of the Actions that a Button click would run, then go into the Page XML and find the action's corresponding XML, and then copy and paste that into the following:


skuid.actions.runActionNode(
   skuid.utils.makeXMLDoc(
       '<action type="sfdc-custom-apex" ... >' +
           '<inputs>' +
               '<input .... />' +
           '</inputs>' +
       '</action>'
   ),
   component,
   context
);

Where component is optional, and context should be an object containing any context information needed for merges within the action definition to function properly, e.g.

var context = {
    model: someModel,
    row: someRow
};

This is not documented, but it is fully supported and (hopefully) will be added to our API docs soon.
Photo of Nepz Solutions

Nepz Solutions

  • 442 Points 250 badge 2x thumb
Here's the code.

This example shows how to send list of strings in request & get back list of strings in response. you can use this pattern for any use case, by serializing/de-serializing any objects to strings back & forth.

JS inline:

(function(skuid) {    var $ = skuid.$;
    $(document.body).one('pageload', function() {
        var myModel = skuid.model.getModel('MyModelId');
        var myComponent = skuid.component.getById('MyComponentUniqueId');
    });

    try {

        var request = '{ ' +
            '"inputs" : [ ' +
            '{ "request" : "value1"}, ' +
            '{ "request" : "value2"} ' +
            '] ' +
            '}';
        console.log(request);

        $.ajax('/services/data/v33.0/actions/custom/apex/ActionsForSkuid', {
            data: request,
            type: 'POST',
            crossDomain: true,
            dataType: "json",
            beforeSend: function(xhr) {
                xhr.setRequestHeader('Authorization', 'Bearer ' + sforce.connection.sessionId);
                xhr.setRequestHeader('Content-Type', 'application/json');
            },
            success: function(response) {
                console.log([response[0].outputValues.output, response[1].outputValues.output]); // ['returnValue1', 'returnValue2']
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log([jqXHR, textStatus, errorThrown]);
            }
        });
    } catch (e) {
        console.log(e);
        alert(e);
    }

})(skuid);


Apex Class:

public class ActionsForSkuid {
    @invocablemethod(label = 'Skuid Exposed API')
    public static List < String > invokeApexAction(List < String > request) {
        System.debug(request[0]); // value1
        System.debug(request[1]); // value2

        return new List < String > {
            'returnValue1',
            'returnValue2'
        };
    }
}