Example of using Salesforce package and record variable

Build a bot that queries Salesforce objects and writes data to a CSV file. The bot also uses a record variable to retrieve data from individual records fetched from Salesforce.

Prerequisites

  • Set up a connected app in Salesforce if you do not have one already.
  • This bot uses the Control Room OAuth connection method. Create a Control Room OAuth connection before you begin to build the bot. For more information, see Salesforce Authentication action.
  • Set up sample opportunity and accounts data in Salesforce. If you do not have sample data, you can import the data from the following CSV files into Salesforce:
Suppose that the Sales team at Acme Solutions Pvt Ltd decides to automate the process of getting all opportunity records with the prospecting status and then assigning them to sales representatives to follow up. The team builds a bot that performs the following tasks:
  • Fetches all opportunities with the Prospecting status from the Opportunity object in Salesforce.
  • Fetches the Account name from the Account object using the Account id retrieved from each opportunity record.
  • Uses a record to capture data from individual opportunity records.
  • Writes the records to a CSV file, which can be further processed to assign sales reps.
The sample bot uses the Salesforce, Record, Loop, and Data Table packages along with record variables to show how to perform the following automation tasks:
  • Queries a Salesforce object using the Salesforce Execute SOQL action.
  • Get an individual record from a Salesforce object using the Salesforce Get record action.
  • Update record variables and reset record variables.
  • Insert records into a Data Table variable.
  • Write a Data Table to a CSV file.

Procedure

  1. Create a bot.
    1. On the left panel, click Automation. A list of available bots and forms is displayed.
    2. Click Create New > Task Bot .
    3. In Name, enter Get Sales Opportunities.
    4. Click Create and Edit.
  2. Authenticate with Salesforce. For this tutorial, Control room OAuth connection is used as Authentication type. The instructions for setting up OAuth connection are beyond the scope of this tutorial. If you are not familiar with setting up OAuth connections, follow the instructions provided on this page or you can use OAuth 2 - Client Credentials as the Authentication type, and provide client id, client secret, Salesforce username, and password within the Salesforce Authentication action.
    1. From the Actions panel, find and add Salesforce > Authentication action to the Bot editor.
    2. In Authentication type, select Control room OAuth connection.
    3. Click Pick and select the OAuth connection you have set up with the following details:
      • Provider type: Salesforce
      • Connection name: the connection you have set up.
      • Token type: Shared.
    4. In API version, enter the version number of the API for the Salesforce connected app. For example, 50.0.
    5. Leave the Session name as Default.
    6. In Mapped variables, click Add variable mapping, and then map the key IntanceUrl to a String variable named InstanceUrl.
  3. To check for authentication errors before executing other tasks, use the Try Catch actions.
    1. From the Actions panel, find and add Error handler > Try action to the Bot editor.
    2. Click the Catch block icon.
    3. In Exception, select Error handler.
    4. In Assign exception message to, click (x) and create a String variable named ErrorMessage.
    5. Add the Message box action within the Catch block.
    6. In Enter the message to display, select the ErrorMessage variable and save the changes.
  4. Query the Salesforce Opportunity object to fetch opportunities records with the Prospecting status .
    1. From the Actions panel, find and add Salesforce > Execute SOQL action within the Try block.
    2. In SOQL query to execute enter the following query.
      SELECT Id, Name, AccountId, Amount, CloseDate, StageName, NextStep FROM Opportunity WHERE StageName = 'Prospecting'
    3. In Include deleted records, click No.
    4. Ensure Default is set as the Session name.
    5. In Save the outcome to a variable, click (x) and create a List variable named list_records_prospecting.
    6. Save the changes.
  5. Loop through the list of retrieved opportunities records.
    1. From the Actions panel, find and add Loop > Loop action after the Execute SOQL action within the Try block.
    2. In Iterator, select For each item in the list.
    3. In list, select the list_records_prospecting variable.
    4. In Assign the current value to variable, click (x) to create variable named dict_record_opportunity with type Any.
      The following is an example of a record that is stored as dictionary. The bot retrieves values only from the Name, Account name, Amount, CloseDate, StageName, and NextStep fields and leaves out the rest.
      {attributes,com.automationanywhere.botcommand.data.impl.DictionaryValue@5d804b9c},{Id,0065j00001Q2ISAAA3},{Name,Opportunity #1},{AccountId,0015j00001BU2hnAAD},{Amount,2000.0},{CloseDate,2023-10-05},{StageName,Prospecting},{NextStep,Call to Discuss}
    5. Save the changes.
  6. Before retrieving values from each field in a record, create two record variables - one for storing the values from each opportunity record and another for resetting the first record variable at the end of each iteration.
    1. In Variables panel, click Create variable.
    2. In Type, select Record.
    3. In Name, enter out_record_prospecting.
    4. Click Create.
    5. Repeat steps a to d to create another record variable with the following details:
      Type: Record
      Name: record_empty
  7. Update the record variable with the Opportunity name from the current record.
    1. From the Actions panel, find and add Record > Add column action within the Loop action.
    2. In Source record variable, select out_record_prospecting.
    3. In Column type, select String, and in Value, enter $dict_record_opportunity{Name}$.
    4. Click specific index, enter 0.
    5. In Destination record variable, select out_record_prospecting.
    6. Save the changes.
  8. The opportunities records retrieved from Salesforce contain only the account IDs and not account names. Retrieve the account name using the account ID for each record.
    1. From the Actions panel, find and add Salesforce > Get record action within the Loop action.
    2. In Object name, enter Account.
    3. Select Record ID and enter $dict_record_opportunity{AccountId}$.
    4. In Fields and data, select List and click Add.
    5. In Type, select String.
    6. In Value at 0, enter Name.
    7. Make sure Default is set as the Salesforce Session name.
    8. In Mapped variables, click Add variable mapping to map the key Name to a String variable AccountName. The Get record call retrieves the account name using key Name and stores it in the AccountName variable.
    9. Save the changes.
  9. Add a new column and update the record variable with the account name.
    1. From the Actions panel, find and add Record > Add column action within the Loop action.
    2. In Source record variable, select out_record_prospecting.
    3. In Column type, select String, and in Value, enter $AccountName$.
    4. Click specific index, enter 1.
    5. In Destination record variable, select out_record_prospecting.
    6. Save the changes.
  10. Add a new column and update the record variable with amount.
    1. From the Actions panel, find and add Record > Add column action within the Loop action.
    2. In Source record variable, select out_record_prospecting.
    3. In Column type, select Number, and in Value, enter $dict_record_opportunity{Amount}$.
    4. Click specific index, enter 2.
    5. In Destination record variable, select out_record_prospecting.
    6. Save the changes.
  11. Update the record variable with CloseDate, StageName, and NextStep.
    1. Add the Add column action to the Loop action and then enter the following details:
      Field Value
      Source record variable out_record_prospecting
      Column type String
      Value $dict_record_opportunity{CloseDate}$
      specific index 3
    2. Add the Add column action to the Loop action and then enter the following details:
      Field Value
      Source record variable out_record_prospecting
      Column type String
      Value $dict_record_opportunity{StageName}$
      specific index 4
    3. Add the Add column action to the Loop action and then enter the following details:
      Field Value
      Source record variable out_record_prospecting
      Column type String
      Value $dict_record_opportunity{NextStep}$
      specific index 5
    4. Save the changes.
  12. Insert the record into a DataTable variable.
    1. To keep track of the iterations and use it as a row index number, create a Number variable named counter and set the default value to 0.
    2. From the Actions panel, find and add Data Table > Insert row action within the Loop action.
    3. In Data table name, click (x) and create a Table variable named table_records.
    4. Click specific index and enter $counter$.
    5. In Row record name, select out_record_prospecting.
    6. Save the changes.
  13. Increment the counter variable by 1.
    1. From the Actions panel, find and add Number > Increment action within the Loop action.
    2. In Enter number, enter $counter$.
    3. In Enter increment value, enter 1.
    4. In Assign the output to variable, select counter.
    5. Save the changes.
  14. Reset the out_record_prospecting record variable before the next iteration.
    1. From the Actions panel, find and add Record > Assign action within the Loop action.
    2. In Select the source record variable, select record_empty.
    3. In Select the source record variable, click Record and select out_record_prospecting. Since the record_empty variable does not have any value, the out_record_prospecting will be reset.
    4. Save the changes.
  15. Write the retrieved opportunity records to a CSV file.
    1. From the Actions panel, find and add Data Table > Write to file action outside the Loop action.
    2. In Data table name, select table_records.
    3. In Enter file name, specify the CSV file path. For example, C:\bots\opportunities.csv.
    4. Select Create folders/files if it doesn't exist.
    5. Click Overwrite existing file.
    6. Save the changes and run the bot.