Let’s Build Choreo Webhook for Salesforce Events

Anuradha Karunarathna
5 min readMay 29, 2022

Then you can write tons of integrations on Salesforce events with low code

User Story:

  • Company XYZ creates its leads (people who are interested in their product/service. ie: potential customers) in salesforce based on user engagement in marketing activities such as completing lead forms, clicking on LinkedIn ads, and registering for company events etc.
  • Once the account manager of company XYZ contacts a lead to have a meeting, show product demos, and convert the lead to an account/contact. Company XYZ needs to communicate that event to a slack group, send an email to an email group, and update marketing systems at the same time.

Today we are going to satisfy the company XYZ’s requirement with Choreo integration platform provided by WSO2.

  1. As the very first step you have to create a Choreo account by clicking “Try Chooreo Now” here https://wso2.com/choreo/ if you don’t have a Choreo account already. Otherwise login to your account.
  2. You can create a separate project in your organization / use the default project to create the webhook component.
Click the +Create button on the Components page
Select Webhook

3.

  • Give a name to your webhook.
  • Select Salesforce as Trigger Type.
  • The only option for Trigger Channel is StreamingEventService.

Salesforce has multiple subscribe options to listen to its’ change data Refer: https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_subscribe.htm

Out of them, the Choreo salesforce webhook supports subscription channel. (A subscription channel is a stream of change events that correspond to one or more entities. You can subscribe to a channel to receive change event notifications for record create, update, delete, and undelete operations.)

Give webhook component details
Select choreo managed repository. Then the written code is managed in a git repo generated by choreo.

4. Once you are given the required details for webhook creation it will take a few seconds to make your component ready to use.

Waiting for component creation

5. Once the component is ready, you will be redirected to the following page. Click on “Edit Code”

Click on the Edit Code button

6. There you could see the listener functions for onCreate, onUpdate, onDelete and onRestore. Since we are working on the Lead object update scenario, we need to edit the onUpdate function.

7. Click on the ‘Show Source’ icon to view the ballerina source code for this logic.

Diagram and source code

8. Before integrating any other systems, let's see what are the details we receive on the update event.

9. Now, click on the “Run” button in the top right corner. It will prompt some configurations. They are salesforce listener configs. If you look at the code we have a configurable value as follows. We have to give actual values to them before executing the program.

username — Salesforce login usernamepassword — Salesforce login password appended with the security token (<password><security token>)channelName — The channel name to which a client can subscribe to receive event notificationsreplayFrom — The replay ID to change the point in time when events are read
`-1` — Get all new events sent after subscription. This option is the default
`-2` — Get all new events sent after subscription and all past events within the retention window
`Specific number` — Get all events that occurred after the event with the specified replay ID

Refer: https://github.com/ballerina-platform/asyncapi-triggers/blob/114aa2ae69b648ed2aa1055349a1a89fd5a412be/asyncapi/salesforce/data_types.bal#L19-L25

Out of the above configs, everything is implicitly known unless channelName and security token and. Let’s find out how to get these config details. Move on 😉

configurable salesforce:ListenerConfig config = ?;

Salesforce Configs

Find the channel Name

10. Let’s configure the Salesforce side to capture data change events.

Click on the setup gear icon near the profile -> Setup.
Then Search ‘data capture’ in the “Quick Find” search box and click on the “Change Data Capture” result that you find.
In the Change Data Capture UI, Search for Lead in the “Available Entities” section and select the object and press the marked arrow to move it to “Selected Entities”. Then press save.

Get the Security Token

11. Navigate to profile setting -> Reset my Security Token -> Click on the “Reset Security Token” button.

Reset Security Token

Then, the new security token will be emailed to you.

Now you can fill the Choreo webhook configurations and run the program. Then Go to salesforce leads and change status/ attributes and see the logs printed in Choreo.

level = INFO module = anuradha/salesforce_webhook message = "{\"Status\":\"Working - Contacted\",\"LastModifiedDate\":\"2022-05-29T09:49:47.000Z\",\"ChangeEventHeader\":\"{commitNumber=224850856080, commitUser=0058d0000024tO5AAI, sequenceNumber=1, entityName=Lead, changeType=UPDATE, changedFields=[Status, LastModifiedDate], changeOrigin=com/salesforce/api/soap/54.0;client=SfdcInternalAPI/, transactionKey=00015f0c-de5f-2a0e-6ebc-9b926a2deab7, commitTimestamp=1653817787000, recordIds=[00Q8d000004DZf4EAG]}\"}"
Info logs for change data

Next, I’ll modify the code bit more to check

  • whether a lead’s status change has happened?
  • If the status is changed get the changed status and particular lead’s id
  • If the changed status is “Clsoed — Converted” Print a log with the lead id and its new status.
  • Here you can integrate any other system to pass this event change. Slack/email / hubspot / Mailchimp likewise 100s of connectors are readily available in Choreo connector store. So you can use low code mode to integrate with other systems / write the ballerina code in the source code itself. It’s your choice

Once this code is executed, the following log will be the output for a lead’s status change.

time = 2022-05-29T09:57:01.922Z level = INFO module = anuradha/salesforce_webhook message = "SF status change: Closed - Converted received for the lead ID: 00Q8d000004CT2XEAW"

If you are done with the code Commit to the repo and Deploy the webhook following the guild https://wso2.com/choreo/docs/tutorials/create-your-first-rest-api/#step-15-commit-the-rest-api-to-github

That’s it 👏 🥳 Try out Choreo more and see how awesome it is 😎

--

--

Anuradha Karunarathna

Technical Lead @ WSO2 | Computer Science and Engineering graduate@ University of Moratuwa, SriLanka