Copado Function Programming Languages

Updated 1 week ago by Copado Solutions

Copado enables you to write a function using different scripting languages. These languages define how a function parameter value is accessed, how a message is printed in the logs, the helper method signature and more.

Copado currently supports three languages:

  • Bash:
    Bash is the default language. If you don’t specify anything at the beginning of your function script, this is the language that will be used.
  • JavaScript (Node):
    If you want to use this language, you need to specify it by writiting the following statement in the first line of the script:#!/usr/bin/env node.
  • Python
    If you want to use this language, you need to specify it by writing the following statement in the first line of the script:
    #!/usr/bin/env python
    import os
    import json

Let’s take a look at each of these languages and click on the relevant tab to display the information.

To access a function parameter, you can use:

parameter="$paramName".

Don’t forget to add a parameter in the Parameters tab for paramName and the default value.

To log a message in the result logs, you can use:

echo "Starting process…".

You can also build helper methods to print a message in the logs and update the Copado function with the progress status in cases where your script is long:

logInfo () {
echo "$1"
}

logInfo "Starting process..."

Here, you can find an example of how to connect to a Salesforce org with the Salesforce Rest API (you need a Salesforce org session Id and an endpoint, see the article to know how to get these values):

SessionID and Endpoint parameters
## Convert the SOAP endpoint parameter to the REST API Data endpoint
URLPath=$(dirname "$Endpoint")
QUERYPATH=${URLPath/Soap\/u\//data/v}
## Query SFDC for impersonations## Sample SOQL Query (SELECT Name From User WHERE Username="$IntegrationUsername")
curl"$QUERYPATH/query/?q=SELECT+Name+from+User+WHERE+Username=%27$IntegrationUsername%27" -H "Authorization: Bearer $SessionID" > userDetails.json

The example below shows how to loop through JSON array of strings from a string parameter (it uses a jq library):

items='["a", "b", "c"]'

for item in $(jq -r '.[]' <<< "${items}"); do

echo "the item is: ${item}"

If you navigate to the result logs you will see the following output:

the item is: a

the item is: b

the item is: c

To access a function parameter, you can use:

const parameter = process.env.paramName;

Don’t forget to add a parameter in the Parameters tab for paramName and the default value.

To log a message in the result logs, you can use:

console.log("Starting process...");

To execute bash command from Node, you can use:

const execSync = require('child_process').execSync;
execSync("echo \"Hello World\"", {stdio: 'inherit', stderr: 'inherit'});;

You can also build helper methods to print a message in the logs and update the Copado function with the progress status in cases where your script is long:

function logInfo(msg) {
console.log(msg);
}

function logError(msg) {
console.log(msg);
execSync("copado --error-message '" + msg + "'");
}

function logProgress(msg) {
console.log(msg);
execSync("copado --progress '" + msg + "'");
}

You can call the above methods in other parts of your script.

To access a function parameter, you can use:

cos.environ['paramName']

To log a message in the result logs, you can use:

print('Hello World')

To execute bash command from Python, you can use:

os.system('echo "Hello World" ')

To pause the execution to wait for async process or callout, you can use:

time.sleep(10


How did we do?