Building a Function

Updated 1 week ago by Copado Solutions

In the article Introduction to Functions, you have learned about functions and the benefits they can bring to your organization. Now that you know what a function is, let's see how you can create a new function.

In order for you to be able to build and run a function, you need the following:

  • The Copado Functions permission set.
  • An API key.
  • An Admin or User license.
  • The Flow User checkbox selected for your user (this is required for non-sys admin users only).
  • If you are not a sys-admin user, you need read and create access to the MC Results object.
  • You also need to safelist some Copado IP addresses, especially if your Git repository is protected by a firewall. For more information about this, please review the IP Addresses article.

To start building your function, follow these steps:

  1. Navigate to the Functions tab and click on New.
  2. In the Information section, you can provide general information about your function:
    1. Give your function a name and provide a unique API name.
    2. Select a function type. The available options are Standard or Custom.
    3. Provide a description of what the function does.
    4. Add a version number, if desired.

Function Information

  1. In the Configuration section, you need to add the image to be used by the function. You can also further configure your function and add a timeout or a callback:
    1. Image Name: An image is a container of pre-installed apps and features needed to perform a particular function. Copado provides different images so that you don’t need to invest time installing and configuring the applications or features required to run a function. For more information about all the images available and what they include, see the article Function Images.

      The function core image is:
    2. Timeout: Every time a function is executed, Copado creates a Result record with the function execution details and sets the status to In Progress until the job is completed. If you want Copado to update the status to Failed if there is no response from the function’s job after a certain period of time (e.g., 60 minutes), you can define it here.
    3. Worker Size: Worker sizes are not supported at the moment. They will be available shortly. For now, leave this field as it is.
      Configuration Section
  2. Copado enables you to execute an action once the function runs, which you can define in the Callback section. You can call an Apex class or a Salesforce flow. For instance, if you are running a scan, you can trigger a flow afterwards that posts to chatter if the scan has been successful. Copado passes to the flow result and function as record variables and status as text variable:

Copado also provides an out-of-the-box flow you can use as a callback:  MCCallbackHandlerTestFlow. This flow updates the progress status of the Result record that is generated when you execute a function. If you want to invoke this flow, you need to add the Copado namespace. It will look like this:

Copado Callback Flow

Callback Execution

Important considerations about callbacks:
Callbacks are executed by an automated process, and automated processes cannot send an email. If you want to trigger a flow that sends a notification, you need to use a Post to Chatter action.

Only autolaunched flows are supported.

If you want to check the debug logs of the callback executed by a function, you need to create a new trace flag and select Automated Process in the Trace Entity Type picklist field. For more information about how to create a trace flag, review Salesforce’s documentation.

If you want to call an Apex class, the class must be a global class.

If you are using a flow or class from a managed package, make sure you add the namespace.
  1. Once you have defined the callback, click on Save.
  2. Next, navigate to the Script tab and provide the script with the commands you would like the function to execute:
    Script tab

The script provided in the example above clones a public Git repository and reviews its size (for additional script samples, please check the article Example Function Scripts).

  1. The minimum requirements to run a function are the script and the image. However, some functions require additional parameters. And what is a parameter? A parameter is a piece of information taken from outside the function that the function needs to be executed. For instance, for the Git repository clone example above, you need to provide a public repository. You can define these parameters in the Parameters tab:
Parameters Tab

Copado supports different types of parameters. For more information about this, review the Dynamic Expressions article.

Alright, you have built your function. Now, you can go ahead and execute it. To learn more about all the options you have to execute a function, review the article Executing a Function.

How did we do?