This use case revolves around syncing the accounts and contacts from Salesforce to Zendesk, and ensuring that the Zendesk entities are kept up to date when the corresponding Salesforce entity is updated. Let us start by breaking down the exact requirements:
- When a new Account is provisioned in Salesforce, a corresponding Organization should be provisioned in Zendesk
- The website and phone number fields from the Account need to be brought over to organization fields in Zendesk
- When a new Contact is provisioned in a certain account, a corresponding User should be provisioned in Zendesk
- The phone number field from the Contact should be brought over to the user field in Zendesk.
- When the Salesforce Account or Contact are updated, the corresponding entities on the Zendesk side must be updated.
The following diagram depicts the use case:
For the Salesforce part, we need to send out two things i.e. Account and Contact. The script for the outgoing part for Account is as follows:
if(entity.entityType == "Account") { replica.key = entity.Id replica.Name = entity.Name replica.Website = entity.Website replica.Phone = entity.Phone replica."tType" = "Account" }
Here we have sent an additional variable called replica."tType" to designate the type of entity we are sending across. This would help on the Zendesk side to decide what entity type we are dealing with.
In order to send over the Contact information, the script is as follows:
if(entity.entityType == "Contact") { replica.key = entity.Id replica.Name = entity.Name def res = httpClient.get("/services/data/v54.0/query/?q=SELECT+Name+from+Account+where+id=%27${entity.AccountId}%27") replica.AccountName = res.records[0].Name replica.Email = entity.Email replica.MobilePhone = entity.MobilePhone replica."tType" = "Contact" }
Here, we run an API call on the Account table to fetch the Account Name of the Contact. This is needed in order to correctly link the Zendesk objects together.
Moving the Zendesk part now, we will be receiving the Salesforce payload, and running custom API calls in order to create the Organization and Users correctly. For this we will need the ZdClient class firstly:
Once we have this class in place, we are able to make any API calls to the Zendesk API (or any other for that matter). The following script now goes and creates the organization and the users:
The above script will create a Zendesk ticket when a new Account/ Contact is received from Salesforce side, and will use a custom field called "Salesforce Account Number" to hold the accountId from SF (to maintain the correlation).
A video demonstration of the solution is as follows:
A complete listing of the scripts: