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.
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.
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, responset1].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(requests0]); // value1
System.debug(requests1]); // value2
return new List < String > {
'returnValue1',
'returnValue2'
};
}
}
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.