Serverless integration with Salesforce using Kumologica
Published: 2021-04-05 14:34:15

Salesforce is a popular CRM platform used by many enterprises. Integration of salesforce modules with different enterprise systems are very common. There are multiple ways by which salesforce objects can be integrated .
* SOQL query (Pulling the records)
* Platform Events (Pushing the events)
* CDC (Pushing the events)
* OutboundMessages (Pushing the events using webhooks)

Most of the real time integrations with Salesforce works with PE(platform events), CDC and OutboundMessages. There are also solutions where SOQL query works in complementary with PE, CDC or OutboundMessages (Webhooks).

In this article we will see how to send a Salesforce outbound message to the target systems using [Kumologica]('https://www.kumologica.com/') flow that runs on serverless infrastructure. For those who are new to Kumologica I would recommend to go through our [articles]('https://medium.com/@kumologica/') and [youtube]('https://www.youtube.com/channel/UCsTDAlhijUFpsfThJu3hF9g') videos to get an insight.

<img src="https://miro.medium.com/max/1400/1*vaBjqAE1TxqmU7kY87DWnA.png"/>

<h2>Concept</h2>

Before we begin let’s try to understand the working mechanism behind [outbound messages in Salesforce]('https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_om_outboundmessaging_understanding.htm').
OutboundMessages in salesforce is part of workflow in salesforce. The underlying mechanism is based on webhook notification. Whenever a change happens in any salesforce objects an associated outbound message is triggered by the workflow rule. The change can be an insert, update or a delete of an salesforce object. The outbound message is configured with a HTTP post endpoint. Salesforce outbound message will invoke the endpoint to send a SOAP payload having the appropriate object fields (eg : Case, Contact, etc).

<h2>Use Case</h2>

In this use case we will be using the Salesforce contact object. Whenever a new contact is created in Salesforce an event is triggered which will be sent to the target system by Kumologica flow. Here the target system is Amazon SQS queue.

<img src="https://miro.medium.com/max/1400/1*o2ojOTamZrB3EFSupwVLPw.png"/>

The HTTP endpoint implementation will be done using Kumologica designer. The Kumologica flow will be then deployed on AWS Lambda. After the deployment we will copy the gateway endpoint which is then configured on the outboundMessage section in Salesforce.

<h2>Prerequisite</h2>

* Salesforce account — For configuring the outbound message connecting contact module.
* Kumologica Designer — [Download]('https://www.kumologica.com/download.html') the designer for building the flow.
* AWS account — For running the Kumologica flow as lambda and for hosting the SQS queue.

<h2>Implementation</h2>

We will be creating ContactInboundService flow in Kumologica designer. This flow will expose the endpoint on the resource path <i>/sfdc</i>.

<b>ContactInboundService flow</b>

<img src="https://miro.medium.com/max/1400/1*7gTDM07G5wPk-M6rtUFvZw.png"/>

<b>Steps<b>

1. Open Kumologica Designer, click the Home button and choose Create New Kumologica Project.
2. Enter name (for example ContactInboundService), select directory for project.
3. Press Create Button.
4. Drag and drop EventListener node to the canvas the provide the following configuration.
<code>
Display Name :  POST /sfdc
Provider : AWS
Event Source : Amazon API gateway
Verb : POST
URL : /sfdc
</code>
5. Add a Logger node and wire with the EventListener node. Provide the following message.
<code>
'Request received - ' & msg.payload
</code>
6. Drag and drop the Set-Property node to the canvas and wire it with the logger node. Provide the following configuration.
<code>
Display Name :  ConvertToString
Rules :
   Set : msg.payload
   To  : J: $string(msg.payload) 
</code>
**Note:** J : is the JSONata expression to be selected from the drop down.
7. Add the SQS node to canvas and wire it with the Set-Propery node. Provide the following configuration.
<code>
Operation : SendMessage
QueueUrl : <<SQS queue url>>
MessageBody : msg.payload
</code>
**Note:** Ensure that you have an SQS queue created in your AWS account and you have the queue url for configuring in the SQS node. Refer : https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-getting-started.html#step-create-queue
8. Add EventListener End node and wire with the SQS node. Provide the following configuration.
<code>
Display Name :  Success
Status Code : 200
Content-Type : application/json
Payload : {"status": "completed"}
</code>

<h2>Deployment</h2>

1. Select AWS icon on the right hand side vertical tab of Kumologica designer, select your AWS Profile.

**Note:** If you haven’t mapped the your local AWS profile with the designer then you may follow the below video to configure it.

[Set profile video](https://www.youtube.com/embed/0HetFCqWQ6Q)

1. Click Connect. If successfully connected will show the rest of the configuration options.
2. Set the Memory to 512mb and Timeout as 20 seconds.
3. Go to “Trigger” section under cloud tab and select the Amazon API Gateway trigger.

<img src="https://miro.medium.com/max/1396/0*7Idwo_QCm4IjPkUT.png"/>

4. Post successful deployment copy the endpoint url from the Kumologica terminal. We will need this endpoint for configuring the OutboundMessage in Salesforce.

<code>
Url eg: https://ii48n22.execute-api.ap-southeast-2.amazonaws.com/test
</code>

**Configuring OutboundMessage in Salesforce**

1. Go to your salesforce Account and search for Outbound Messages.
2. On the Outbound Message screen , create a “New Outbound Message”
3. Select the object as “Contact” and Click Next. Provide the following details.

<img src="https://miro.medium.com/max/1400/1*DdwV8-XszMIkX13b8LNNQQ.png"/>

<img src="https://miro.medium.com/max/1400/1*xPoY0oKuc53FmeDlh-3Qyg.png"/>
4. Click Save. Now you have created an Outbound Message.
5. Let’s map the Outbound Message to the workflow rule.

<img src="https://miro.medium.com/max/1400/1*QRbmkUuUqr7xAqa41N0TQw.png"/>

<img src="https://miro.medium.com/max/1400/1*IZerJUHOostHoc194JTgrA.png"/>

The above rule configuration ensures that anytime a contact is created with the description that starts with “ABC” the workflow will invoke the Outbound Message.
Now we will map the “ContactWebhook” OutboundMessage we have created in the earlier step to this workflow rule we are configuring.
6. From “Add Workflow Action” click on “Select Existing Action”.

<img src="https://miro.medium.com/max/1400/1*baHKMitGJfTwtJDio6euqw.png"/>

From the type select “OutboundMessage”.
<img src="https://miro.medium.com/max/1400/1*kcV9bM916pnL-ATVahxelA.png"/>

Now you will see the “ContactWebhook” in the Available Actions list. Add it and Save.
7. Now we are ready to rock n roll.

<h2>Testing</h2>

1. Go to the Salesforce contact module and create a new contact.
2. Go to the AWS account and look into the SQS queue. You will observe that a SOAP message is now available in the queue.