Skip to content

CRM

Query CRM

This example shows how to read CRM Contacts and send an email to each record returned.

Query CRM
LastName=
FirstName=
Email=
CSVLine=
CSV=
 
// Read some records from CRM Contacts
CSV=Query CRM Entitieseu30.salesforce.comSELECTEmail,FirstName,LastNameFROMContactWHERELastActivityDateGreater Than%SQLDateYesterday%LIMIT 10
 
// We now have CSV data. Loop through each line in the CSV variable.
For EachLine In%CSV%[Assign To: CSVLine]
// Extract specific fields from the CSVLine
Parse CSV Line%CSVLine%Set%Email%=Col1,%FirstName%=Col2,%LastName%=Col3
If%Email%Is A Valid Email AddressThen
Send EmailTo%FirstName% %LastName% <%Email%>"Welcome New Customer"
End If
Next Loop

First we use the Query CRM Entities action to to read the Email, FirstName & LastName columns from the Salesforce CRM Contact table. The results are returned as CSV text to the CSV variable (there is also the option of returning as Json or Markdown).

We then use the For..Each action to loop on each Line in the CSV variable.

Inside the loop we use the Parse CSV Line action to get the Email, FirstName & LastName values and assign them to variables.

We can then use the Send Email action to send an email.


Add Attachments To A Salesforce Contact

In this example we want to check each incoming email. If a contact record exists in Salesforce with the from address of the incoming email we want to add any PDF attachments to the Salesforce contact record.

Salesforce Add Attachments
// Automation Actions Follow
UserId=
Path=
FileName=
Count=
FileExtension=
ContactID=
 
// Check if a Salesforce Contact exists for the incoming message From address
Get CRM Entityeu30.salesforce.comContactWHEREEmailEqual To%Msg_FromEmail%SET%ContactID%=Id
 
If%ContactID%Is Not BlankThen
// Get UserID
Get CRM Entityeu30.salesforce.comUserWHEREEmailEqual To%MyUserEmail%SET%UserId%=Id
 
// For each attachment
For EachAttachment[Assign To: FileName, Path]
FileExtension=Extract File Extension(%Path%)
If%FileExtension%Equal TopdfThen
// We only want to add PDF attachments
// Add attachment to Salesforce contact.
// Set the OwnerId to the UserId and the ParentId to the ContactID
Update CRM Entityeu30.salesforce.comAttachmentCreate New EntitySETIsPrivate=False,OwnerId=%UserId%,Body=%Path%,Name=%FileName%,ParentId=%ContactID%
End If
Next Loop
Return%ContactID%
End If

First we use the Get CRM Entity action to lookup a contact from Salesforce where the Email field is equal to %Msg_FromEmail%. If a contact is found the %ContactID% variable will be assigned the Salesforce contact id.

If a contact is found (the %ContactID% variable is not blank), we start an If block.

Each attachment must be assigned an OwnerId, this is the id of the user that is adding the attachment. We need to use the Get CRM Entity action to lookup the user record to get the user Id.

Note: You could save the user id as a Solution Constant to save having to look it up each time.

We then use the For..Each action to loop on each attachment. And then the Update CRM Entity action to add a record to the Attachments table. The Body field of the Salesforce table is a binary type. For binary types, if the assigned value is a path the actual file contents will be read and assigned.