Fetch Records from Salesforce

Table of Contents

 Overview

The salesforce.sales.getRecords() task is used to fetch records from any of the supported modules in your Salesforce account, to your Zoho Creator Application. For example, you can fetch records from Salesforce modules like Leads, Contacts, Accounts etc.  To connect Zoho Creator with Salesforce, add a new connection from the Setup - Connections page. 

 Supported Modules

  • Lead
  • Contact
  • Account
  • Case
  • Solution
  • Product
  • Campaign

 Syntax

<salesforceResponse> = salesforce.sales.getRecords(<salesforceConnectionName>, <salesforceModuleName>, <criteria>, <ColumnsToBeSelected>);

where, 
<salesforceResponse> is the map variable returned as response. It contains the details of the record that needs to be fetched. 
<salesforceConnectionName> refers to the connection name used to connect Zoho Creator with Salesforce. Create a new connection from the Setup - Connections page.
<salesforceModuleName> refers to the module name in Salesforce where the record is created. 
<criteria> is the optional parameter based on which the records wll be fetched. The criteria must be in the format <attribute name> = <value>. For example, "Name = 'Henry'"
<columnsToBeSelected> is the optional parameter which specifies the name of the attributes to be selected.

 Example

A sample script to fetch a record from the Salesforce accounts module is given below. You can execute this script from a Zoho Creator form by adding it to the required Form Actions block.  For example, to fetch a record from the accounts module,

  1. Select the Form from your Dashboard page.
  2. Go to the required Form Actions block.
  3. Add the script to the editor area.
  4. Click on Save Script to save the script definition.

    //returns a list of records from the Salesforce "Accounts" module
    SalesforceResponse = salesforce.sales.getRecords("CRM", "Account");

    //returns a list of records with Name = "Henry"
    SalesforceResponse = salesforce.sales.getRecords("CRM", "Account","Name = 'Henry'");

    //returns the Name, ID and Industry of the records from the Salesforce "Accounts" module with Name ="Henry" 
    fieldList = List();
    fieldList.add("Name");
    fieldList.add("Id");
    fieldList.add("Industry");
    SalesforceResponse = salesforce.sales.getRecords("CRM", "Account", "Name = 'Henry'",fieldList);

Note

  • You can add the script directly to the editor area by selecting Free-flow scripting link displayed on the top-right corner of the script builder ( or ) use the drag-and-drop functionality in Script Builder.

Response Format

The response returned by Salesforce will be in the format given below:

{
"done":true,
"records":"[{\"Name\":\"Henry\",\"Id\":\"00190000010bZ88AAE\",\"attributes\":
{\"type\":\"Account\",\"url\":\"/services/data/v20.0/sobjects/Account/00190000010bZ88AAE\"},\"Industry\":\"Chemicals\"}]",
"totalSize":1
}

If the execution fails due to Malformed Query,the response will be in the following format:

{
"message":"\nSELECT * FROM Account where Name='John'\n ^\nERROR at Row:1:Column:7\nunexpected token: '*'",
"errorCode":"MALFORMED_QUERY"
}

 Using Script Builder

To add the script using the drag-n-drop functionality in Script Builder,

  1. Select the Form from your Dashboard page.
  2. Go to the required Form Actions block.
  3. Click on Scrip Builder from the top-right corner of the page.
  4. Drag and drop the call function in the editor box. 
  5. Select Integration tasks
  6. Select Service as Salesforce
  7. Select Function as Get Records.
  8. Select the Connection Name to connect with Salesforce. Click on +Create to create a new connection or create a new connection from the Setup - Connections page.
  9. Select the module name in Salesforce, from where you need to fetch the record.
  10. Specify the criteria based on which the records will be fetched. This is an optional parameter.  
  11. Specify the columns to be fetched. This is an optional parameter.
  12. Specify the response variable that will hold the response returned by Salesforce.
  13. Click on Done to insert the call function task.


Upon submission of the form, the specific record gets fetched from Salesforce based on the given module name and criteria. The response variable will contain the response returned by Salesforce.