Create a Salesforce Function

You're ready to create your first Function. Run this command in the project root directory:

  • -n functionName the name of the Function. Names must start with a letter and contain only lowercase letters and numbers.
  • -l language the programming language to use.

In VS Code, from the Command Palette, choose SFDX: Create Function. Enter a Function name. For this tutorial use myfunction and select the JavaScript language.

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 file 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: