Integration-Salesforce-api-access

Salesforce API Access

To access the Salesforce API via JavaScript functions you have to complete the following steps. Below you will also find some code examples. You can also find these examples as a template when creating a new function in CustomJS. By the way, here you can get a Salesforce Trial version with which you can also test the API integration: Salesforce Developer Edition Signup

Setup

1: First you need to create a connected app:

  • -  Setup -> App Manager
    • -  Create a "New Connect App"
    • -  Enable OAuth Settings
    • -  Simply insert any callback URL
    • -  Selected "Full Access" as OAuth Scopes
    • -  Click on Save

2: Grab Consuer Credentials

  • -  Setup -> App Manager
    • -  Select "View" from the context menu of your connected app
    • -  Click on "Manage Consumer Details"
    • -  You will probably have to re-authenticate your account with a code that you receive by email
    • -  Copy the "Consumer Key" and "Consumer Secret" into CustomJS variables of your function

3: Then you have to allow OAuth with username-password flows:

  • -  Setup -> Identity -> OAuth and OpenID Connect Settings
    • -  Enable Allow OAuth Username-Password Flows

4: Now you need a personal security token

  • -  Setup -> My Personal Information -> Reset My Security Token
    • -  Click on "Reset Security Token"
    • -  Now you will receive a token by email and you can then save it in a CustomJS variable called "securityToken", for example

5: Find out the API version

  • -  Setup - Integrations - API
    • -  Click on "Generate Enterprise WSDL"
    • -  Recognize the API version and adapt the code
      Connected App
      Connected App

Code Example

const axios = require('axios');

const instanceUrl = 'https://XYZSSDD-dev-ed.develop.my.salesforce.com';
const apiVersion = '61.0'; 

const accessToken = await getAccessToken();
await updateOpportunityField(accessToken, 'NextStep', 'Buy CustomJS');

/////////////

async function getAccessToken() {
    const tokenUrl = 'https://login.salesforce.com/services/oauth2/token';
    try {
        const response = await axios.post(tokenUrl, null, {
            params: {
                grant_type: 'password',
                client_id: variables.consumerKey,
                client_secret: variables.consumerSecret,
                username: variables.username,
                password: variables.password + variables.securityToken
            }
        });
        return response.data.access_token;
    } catch (error) {
        console.log('Error getting access token', error.response.data);
        throw error;
    }
}

async function updateOpportunityField(accessToken, fieldName, fieldValue) {
    try {
        await axios.patch(
            `${instanceUrl}/services/data/v${apiVersion}/sobjects/Opportunity/${input.opportunityId}`,
            { [fieldName]: fieldValue },
            {
                headers: {
                    Authorization: `Bearer ${accessToken}`,
                    'Content-Type': 'application/json'
                }
            }
        );
    } catch (error) {
        console.log('Error updating opportunity', error);
        throw error;
    }
}

Connected App
Connected App


Use Cases