Executing a Function

Updated 5 days ago by Copado Solutions

If you have gone through the Introduction to Functions article, you may already know what a function is and how you can build one.

In this article, you will learn how to execute a function. Copado enables you to execute a function from different places such as a Function record or a Salesforce flow. Let’s dig deeper into each of the options you have to run a function.

Copado provides monthly free function execution time. If you need more, contact your customer success manager.

Executing a Function from the Function Record

Once you have finished building your function, you can run it straightaway from the Function record and check if everything is working as expected by clicking on Execute Function:

Execute Function Button

When you execute a function, if any parameter needs to be passed (either because it was defined in the parameters tab, or you want to add it during the execution), you need to enter a context id, which is the Salesforce Id of the record you will be retrieving these parameters from:

Function execution modal

Once the function is executed, Copado creates a Result record with the job status. You can see this status on the Function Results chart on the Function record (if you cannot see this chart see the Adding the Functions Result Chart to the Function Record section below):

function result chart

Alternatively, you can navigate to the Results record from the Execution History tab. This Result record includes the logs attached so that you can further troubleshoot the issues if the job has failed.

Result Record

Adding the Functions Result Chart to the Function Record

The Function record includes a Function Results chart that displays information about the function execution. To ensure this chart is displayed, follow these steps:

  1. Click on the gear icon at the top right corner of the Function record page, and then on Edit Page:

Edit Page button

  1. Click on the Function Results section and set the Filter By field to Function ID:
Filter By picklist

  1. Save the changes and click on Activate.
  2. Next, click on Assign as Org Default.
  3. Select the device where you want to make this page available (desktop/phone) and click on Next.
  4. Lastly, click on Save.

Executing a Function from a Salesforce Flow

If you want to execute a function as part of your Salesforce flow, you can easily do this using the Invoke Copado Function Apex action. You need to provide the functions’ API name and the required parameters, if any. You can find this information in the Function record:

Invoke Copado Function

If your function requires additional parameters, you can specify them in the Parameters (JSON) input field. Simply enter a json array with the list of parameters and their values. For example:

[{"name":"example-name","value":"example.value"}, {"name":"example-name2","value":"example.value2"}]

If you need to reference a parameter you have defined in the flow, you can use the Parameters field. Additionally, if you want to reference a set of parameters you have defined in a JSON field outside the flow, you can use the Parameters (JSON) field. If you provide a value for both fields, Copado will apply the values in the Parameters (JSON) field.

If the parameter values depend on a variable from the Salesforce flow, you can also create a flow resource of type Formula and enter a value like this:

[{"name":"example-name","value":"example.value"}, {"name":"currentDate","value":"{!$Flow.CurrentDate}"}]

Flow resource

Then, simply select the formula resource as the variable in the ParametersJSON field of the Invoke Copado Function Apex action.

If you want to use the result status component in a screen flow to see the progress when you execute a function, you can configure it as follows:

  • Component: resultStatusMonitor
    • Variables: 3 input variables:
      • recordId
        • Resource Type: Variable
        • Data Type: Text
        • Id of the Result record returned after executing the function.
      • objectAPIName
        • Resource Type: Variable
        • Data Type: Text
        • Currently, this option is only available for the Result object (copado__Result__c).
      • showDetailLink
        • Resource Type: Variable
        • Data Type: Boolean
        • If you want to display a link to the Result record, set this variable to {!$GlobalConstant.True}. Otherwise, set it to false.
          resultStatusMonitor flow component

Executing a Function as Part of a Deployment

One of the main purposes of a function is to automate certain tasks as part of a deployment. While you can use a Salesforce Flow step to trigger jobs in cloud services, if you want, for instance, to execute a test with a testing tool from an open-source library, you need to use a function.

To run a function as part of your deployment, you can use the Function step in an Advanced (multi-step) deployment or include this step in an automation template to run it as part of your quality gate process if you are working with Copado Continuous Delivery.

Function Deployment Step

Leveraging System Properties

A system property is a value that is stored in Copado and can be referenced in a function. System properties are similar to environment variables; however, they can not only be linked to environments but also a pipeline, a user or any object record.

When you create a function, you can reference system properties in the Parameters section, and these parameters are replaced based on the context once the function is executed.

For more information about this feature, please check out the article Dynamic Expressions.

How did we do?