RetrieveSalesforceObjects Function

The RetrieveSingleSalesforceObjects function returns a rowset from an object in Salesforce (both standard and custom objects). You need to have the connector enabled and configured correctly in order for this AMPscript function to work. You do not need to have the object synced via Synchronized Data Extensions. But you do need to use Row function and Field function in conjunction to extract the data and use them in AMPscript variables.

Syntax:

                    

RetrieveSalesforceObjects(1,2,3,4,5)
1 = Salesforce Object: Account, Contact, Lead, Opportunity (or custom object)
2 = Name of fields to retrieve (comma separated if more than one field)
3 = Field name to use in WHERE clause (only one field)
4 = Operator (=, >, <, !=, <=, >=) to compare field in (3) to value in (5)
5 = Value use to evaluate 

                

Example:

                    

%%[
set @rs = RetrieveSalesforceObjects(‘Account’, ‘FirstName, LastName’, ‘PersonContactId’, ‘=’, @subscriberKey)

set @rc = rowcount(@rs)
IF @rc > 0 THEN
  SET @row = ROW(@rs,1)
  SET @FirstName = FIELD(@row,”FirstName”)
  SET @LastName = FIELD(@row,”LastName”)
ENDIF
]%%
Hi @FirstName @LastName!

                

Output:

Hi John Doe!

Explanation:

The RetrieveSalesforceObjects function is used to retrieve data from Sales or Service Cloud. The @rs variable to used in the retrieve function and pulling from the Account object. The fields we want to retrieve from Account are the FirstName and LastName fields.

In order to find the correct records on the Account object, in the RetrieveSalesforceObjects function, we are using the PersonContactId. In this example, this is a Person Account. Using the @subscriberkey and having the PersonContactId equal that value will allow us to retrieve the correct record on the Account object.

The Row and Field functions need to be used with the RetrieveSalesforceObjects function. First we are checking if the retrieve function has and rows retrieved. Using RowCount, the AMPscript is setting the @rc variable to check how many rows RetrieveSalesforceObjects function retrieves.

Then using an If function, if the number of rows retrieved is greater than zero then the AMPscript uses the Row function to return the rowset. The Field function then returns the specified field in that row. In this case, it is the FirstName and the LastName.

The RetrieveSalesforceObjects function finds the record on the Account object and retrieves the FirstName of “John” and the LastName of “Doe.”

Leave a Reply

Your email address will not be published. Required fields are marked *