How to use Salesforce API with Robocorp and Robot Framework

Salesforce logo

Get the code and run this example in your favorite editor on our Portal!

Salesforce provides customer relationship management (CRM) service and also provides enterprise applications focused on customer service, marketing automation, analytics, and application development. Wikipedia - Salesforce

You can build Salesforce API automations with Robocorp. This article introduces the basics.

Prerequisites

  1. Register a Salesforce developer account (it's free).
  2. In Salesforce: View profile -> Settings -> Reset My Security Token
  3. Set up Robocorp vault:
{
  "salesforce": {
    "username": "The username of the account you registered.",
    "password": "The password of the account you registered.",
    "token": "The token from the security token reset mail."
  }
}

Example Salesforce API robot

  • Authenticate to Salesforce with a username, password, and a security token.
  • Create a new Salesforce object.
  • Query objects using Salesforce Object Query Language (SOQL).
  • Describe a Salesforce object by type.
  • Describe all picklist values for a Salesforce object field.
  • Get the metadata for a Salesforce object.

For more information about available features and keywords, see RPA.Salesforce library documentation.

*** Settings ***
Documentation       Salesforce API examples.
...                 Prerequisites: See README.md

Library             Collections
Library             RPA.Robocorp.Vault
Library             RPA.Salesforce
Library             String
Library             RPA.Tables

Suite Setup         Authenticate
Task Setup          Generate random name


*** Tasks ***
Create a new Salesforce object (Opportunity)
    # Salesforce -> Setup -> Object Manager -> Opportunity -> Fields & Relationships.
    # Pass in data as a dictionary of object field names.
    ${account}=
    ...    Salesforce Query Result As Table
    ...    SELECT Id FROM Account WHERE Name = 'Burlington Textiles Corp of America'
    ${object_data}=
    ...    Create Dictionary
    ...    AccountId=${account}[0][0]
    ...    CloseDate=2022-02-22
    ...    Name=${RANDOM_NAME}
    ...    StageName=Closed Won
    ${object}=    Create Salesforce Object    Opportunity    ${object_data}
    ${opportunity}=    Get Salesforce Object By Id    Opportunity    ${object}[id]
    Log Dictionary    ${opportunity}

Query objects using Salesforce Object Query Language (SOQL)
    # Salesforce -> Documentation -> Example SELECT Clauses.
    # Salesforce -> Setup -> Object Manager -> <Type> -> Fields & Relationships.
    ${opportunity}=
    ...    Salesforce Query Result As Table
    ...    SELECT AccountId, Amount, CloseDate, Description, Name FROM Opportunity
    ${list}=    Export Table    ${opportunity}
    Log List    ${list}

Describe a Salesforce object by type
    ${description}=    Describe Salesforce Object    Opportunity
    Log Dictionary    ${description}

Describe all picklist values for a Salesforce object field
    ${description}=    Describe Salesforce Object    Opportunity
    FOR    ${field}    IN    @{description}[fields]
        IF    "${field}[name]" == "StageName"
            Log List    ${field}[picklistValues]
        END
    END

Get the metadata for a Salesforce object
    ${metadata}=    Get Salesforce Object Metadata    Opportunity
    Log Dictionary    ${metadata}


*** Keywords ***
Authenticate
    ${secret}=    Get Secret    salesforce
    Auth With Token
    ...    ${secret}[username]
    ...    ${secret}[password]
    ...    ${secret}[token]

Generate random name
    ${random_string}=    Generate Random String
    Set Suite Variable    ${RANDOM_NAME}    Random name ${random_string}

Salesforce Lightning automation

Using the Salesforce API is the preferred way to build robust automations. APIs are versioned and rarely introduce breaking changes. Always consider API first when automating your processes.

In cases where you need to build your automation against a web application, Robocorp also supports that.

Check out the Salesforce Lightning Automation With Robot Framework Browser example robot.

May 5, 2022