This is a callable class that runs ad-hoc JavaScript from Salesforce.
It brings any results back into the org without consuming API calls.

This image has an empty alt attribute; its file name is 48-deploy-to-salesforce.png

Install – /packaging/installPackage.apexp?p0=04t7F000003iruR
Docs – github.com/bigassforce/streams/wiki/javascript-service

The service can run synchronously or asynchronously:

// this is apex
Streams.Events.enqueue(new Map<String,Object>{
    '_immediate' => true, //sync
    '_javascript' => your_js_here,
    '_to' => 'Streams.Services.JavaScript'
});

In your lambda execute method, return a collection of DML intents.
The intents are handled like a Unit Of Work, described in Apex Intents

// this is javascript
exports.handler = async function(event, context) {

    // account will be inserted
    var account = {attributes:{type:'Account'}}
    account.Description = 'hello world'
    account.Name = 'ACME'

    // event will go to next step
    return [account, event];

}

Amazon config

  1. Sign up for an Amazon Web Services account
  2. Click Setup > My Security Credentials
  3. Access Keys > Create New Access Key

Salesforce config

Create two Named Credentials:

Name: IAM
URL: https://iam.amazonaws.com/
Identity Type: Named Principal
Auth Protocol: AWS Signature V4

AWS Access Key ID: [your key here]
AWS Secret Access Key: [your secret here]
AWS Region: us-east-1
AWS Service: iam

Name: Lambda
https://lambda.us-east-1.amazonaws.com/
Identity Type: Named Principal
Auth Protocol: AWS Signature V4

AWS Access Key ID: [your key here]
AWS Secret Access Key: [your secret here]
AWS Region: us-east-1
AWS Service: lambda

Here’s an example of a synchronous AWS Lambda that fetches data from its own HTTP callout. It doesn’t immediately return – instead, a Promise is used to wrap all the logic. The promise delays the return until the async code finishes.

// this function is executed on AWS Lambda
exports.handler = async function(event, context) {
    
    // from NodeJS standard lib
    const https = require('https')
    
    // hold response
    var responseBody = ''
    
    // demo Quote Of The Day service
    var endpoint = 'https://quotes.rest/qod'
    
    // read how promises work here - https://www.promisejs.org
    var promise = await new Promise(function(resolve, reject) {
        
        // init the HTTP GET request
        var request = https.get(endpoint, function(response) {

            // when chunks received on the wire, build up the body
            response.on('data', function(chunk) {responseBody += chunk})

            // when EOF, return the data!
            response.on('end', function() {

                // prepare intents
                var intents = []

                // add an intent with response data
                intents.push({quotes: JSON.parse(responseBody)})

                // respond to SFDC
                resolve(intents)
            })
        })
        
        // or, error HTTP response: reject({statusCode: 500})
        request.on('error', function(e) {throw 'error: ' + e})
    })
    
    // AWS holds the socket open
    return promise
}

Related Posts

Last modified: 20th July 2020

Comments

Brilliant!!

Write a Reply or Comment

Your email address will not be published.