Skip to content

Salesforce Example

The code below is a rough example of what a process connecting to Salesforce could look like that makes a basic request using the tokens from script.getOAuth.

INFO

This process requires a Standard Integration to have been set up with Salesforce - see Creating an Integration.

Process Code

js

function pre() {
    //this function is called once before the processes is executed.
    //Use this to setup prompts.
    script.log('process pre-execution parameters parsed.');
}

// Endpoints:
// https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_discoveryresource.htm
function begin() {
    script.log('process execution started.');
    const API_VERSION = "v54.0";
    const strAuthData = script.getOAuth("CONNECTOR_NAME");
    if (strAuthData == "") {
        return;
    }

    const authData = JSON.parse(strAuthData);
    const path = `/services/data/${API_VERSION}/sobjects/`;
    const response = web.get(instanceUrl + path, {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${authData['access_token']}`
    });

    if (response.status >= 200 && response.status < 300) {
        console.log("Request succeeded.");
        console.log(response.body);
        const body = JSON.parse(response.body);
        console.log(body);
    } else if (response.error) {
        // An error occurred while sending the request (e.g., network error, timeout, etc.).
        console.log("Error occurred while sending the request: " + response.error);
    } else {
        // The request failed with a non-success status code.
        console.log("Request failed with status code: " + response.status);
        console.log(response.body);
    }
}

function data(record) {
    //this function is called once for each line of data on the second cycle
    //use this to build dimensions and push data into cubes

}

function end() {
    //this function is called once at the end of the process
    script.log('process execution finished.');
}