Executing a Function From Apex

Updated 5 days ago by Copado Solutions

Copado allows you to execute a function from multiple places. In this article, you will learn how to run a  function from Apex.

There are two options to execute a function from Apex:

  1. Using Apex Anonymous.
  2. From a custom Apex class.

All you need to do is include the below snippet in your custom Apex class or execute it in the Developer Console (Debug > Open Execute Anonymous Window):

copado.RunCopadoFunction.InvocableVariables request = new copado.RunCopadoFunction.InvocableVariables();
request.functionApiName = 'TestCredential';
request.contextId = 'contextId';
request.parametersJSON = '[{"name": "key1", "value": "value1"}, {"name": "key2", "value": "value2"}]';
// request.resultCallback = 'SampleResultCallback';
List<String> result = copado.RunCopadoFunction.execute(new List<copado.RunCopadoFunction.InvocableVariables> { request });
String resultId = result[0];

Let’s take a look at the different lines of code included in this snippet:

  1. functionAPIName (String)
    This is the only value required. All other values are optional.
  2. ContextId (String)
    If your function uses dynamic expressions, you need to provide a context.
  3. parametersJSON (String)
    List of parameter values required by the function provided as a JSON string.

If you prefer to use an object-based list of parameters instead of a JSON string, you can use the following example to build your list of parameters:

List<copado.Parameter> parameterList = new List<copado.Parameter>();
copado.Parameter parameter1 = copado.Parameter(name=’key1’, value=’value1’);
parameterList.add(parameter1)
copado.Parameter parameter2 = copado.Parameter(name=’key2’, value=’value2’);
parameterList.add(parameter2)
request.parameters = parameterList;
  1. showValidationErrors (Boolean)
    Return validation errors when you execute the function.
  2. resultCallback (String)
    Name of the Apex class that will be executed as a callback. This is different from the function callback. If you have a function callback and also provide a result callback, Copado will trigger both callbacks. 


How did we do?