Create a Salesforce Function

Now that you have set up the necessary object permissions in your scratch org, you’re ready to create your first Salesforce Function:

From the command palette run the command: SFDX: Create Function Enter a Function name (Function names must be lower case letters or numbers and start with a letter -- for this tutorial use "myfunction"), and select a language for your Function. Choose JavaScript (options include JavaScript and Java). Your project now contains the basic scaffolding that contains supporting metadata and a default boilerplate code for your first Function. Now let's take a closer look at the files that were created:

  • A new directory named after your Function name is created, along with default configuration files.
  • A package.json file for JavaScript contains information about dependencies.
  • A project.toml file that contains Function metadata information. See Developer Guide: Function Metadata TOML Files for more details.
  • index.js that contains some rudimentary Function code. This is your primary source code file. It's preconfigured with an entry point method that has parameters used to pass payload data to the Function and to communicate with the invoking org The default function in index.js logs the received payload, issues a query for Account data, and logs the query results.

The function parameters are:

eventAn Event object that describes the triggering event and contains event data. This parameter is also sometimes called the 'payload.'
contextSalesforce org context for reading from and writing to Salesforce. The context is pre-configured to be authenticated to the invoking org.
loggerSalesforce logger for the function.

Edit index.js and add JavaScript code that implements your Function. The Function in this example uses the Salesforce SDK for Node.js Functions to insert a new Account record in the scratch org, and then queries all Account records with the given fields. The new Account's name is populated from the name field of the payload:

This code is the same as the code used in the Context_SalesforceSDK_JS sample.