In this doc, we explain how we can use **AWS Lambda function** to publish a **Salesforce platform event**, which can do a **lead conversion** and a notification. ### Here is the sequence: 1. AWS lambda function ```sfpe``` publishes a platform event - ![event-bus](https://mohan-chinnappan-n.github.io/sfdc/img/pe/pe-1.png) 2. Salesforce gets that event on the **event bus** and executes event trigger 3. The event trigger performs Lead Conversion and sends Notifications and Chatter post about the completion ![pe-seq](img/pe-seq-1.png) - Lead Conversion ![lc-1](img/le-pa-1.png)
- Create Platform Event Object **LCTest__e** ![pe-1](img/pe-1.png) - Create Platform Event Trigger for **LCTest__e** ``` trigger leadConvert on LCTest__e (after insert) { // TODO: Bulkify // TODO: Check for the msg__c content and do the action based on that String msg = Trigger.new[0].msg__c; String fname = Trigger.new[0].fname__c; String lname = Trigger.new[0].lname__c; String cname = Trigger.new[0].cname__c; Lead myLead = new Lead(LastName = lname, FirstName= fname, Company= cname ); insert myLead; Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadId(myLead.id); LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; lc.setConvertedStatus(convertStatus.MasterLabel); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); // ref: https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_api_considerations.htm /* No Email Support from a Platform Event Trigger: Sending an email message from a platform event trigger using the Messaging.SingleEmailMessage class is not supported. The email can’t be sent because the sender is the Automated Process entity, which has no email address. */ // Let us do FeedItem post FeedItem post = new FeedItem(); // userId can come from the event payload post.ParentId ='0053h000000IJFeAAO'; post.Body = msg; insert post; } ```
- Create node js project with the following code in index.js - ![aws lambda index.js](img/aws-lambda-sf-pe-4.png) ``` //--------------------------------------- // Code for the aws lambda function sfpe // Mohan Chinnappan (mar-12-2020) //--------------------------------------- const fetch = require('node-fetch'); exports.handler = async (event) => { await sendPOST(); return { statusCode: 200, body: "Ok"}; }; async function sendGET() { const url = "https://mohansun-fsc-21.my.salesforce.com/services/data"; const params = { method: "GET", mode: "cors", headers: {"Content-Type":"application/json" }, }; await fetch(url, params); } async function sendPOST() { const sobj = 'LCTest__e'; const url = `https://mohansun-fsc-21.my.salesforce.com/services/data/v46.0/sobjects/${sobj}`; // access token can be passed from outside (via event...) const at = `00D3h000000DC8N!ARcAQAEoNV.jGPdWoODD.GeAp.tzVqKQt_Cci1lrl91CL__WosVlAfOLCKx9tjIuI.I7JWablqx9pu8q209Y2434234234`; const postbody = { "msg__c": "Lead Conversion via AWS Lambda completed", "fname__c": "Johnny", "lname__c": "Sailer", "cname__c": "Jonny Sailer Sons" } ; const params = { method: "POST", mode: "cors", headers: {"Content-Type":"application/json", "Authorization": `Bearer ${at}` }, body: JSON.stringify(postbody) }; await fetch(url, params); } ``` - Node Project Folder ``` $ tree . ├── index.js ├── l_app.js ├── node_modules │ └── node-fetch │ ├── CHANGELOG.md │ ├── LICENSE.md │ ├── README.md │ ├── browser.js │ ├── lib │ │ ├── index.es.js │ │ ├── index.js │ │ └── index.mjs │ └── package.json ├── package-lock.json ├── package.json └── sf-pe.zip ``` - package.json ``` $ cat package.json { "name": "aws-lambda-sf", "version": "0.0.1", "description": "aws lambda and salesforce", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "aws", "lambda", "salesforce" ], "author": "mohan chinnappan", "license": "MIT", "dependencies": { "node-fetch": "^2.6.0" } } ``` - Lambda function Permission ![aws lambda perm](img/aws-perm-1.png) - Event to trigger the Lambda function ![aws lambda event](img/aws-lambda-event-1.png)
- Accounts before the Platform Event - ![pe-lc-1](img/aws-lambda-sf-pe-1.png) - Running the Lambda function - ![lambda results](img/lamba-resutls-1.png) - Accounts after the Platform Event - ![pe-lc-2](img/aws-lambda-sf-pe-2.png) - ![pe-lc-5](img/pe-lc-5.png) - Notifications - ![pe-notification-demo](img/pe-aws-lam-1.gif)` - ![pe-chatter msg](img/lc-pe-aws-chatter-1.png) - Email - ![chatter email](img/pe-aws-chatter-email.png)