Create a DX Project

A Salesforce DX project is a pre-defined structure for describing your org's metadata, code and configuration in a format that can be controlled and shared using a version control system. To begin developing a Function, create a new DX project in the directory where you want your Salesforce Functions project to be located:

  • -n projectname (required) name of the generated project

The CLI creates the project directory structure for you. The project contains base project configuration and scratch definition files, and directories for your tests and sample data sets.

For more details on creating DX projects, see Create a Salesforce DX Project.

Add a New Function to the Project

You are now ready to create your first Function. Initialize a new function named myfunction to generate the basic scaffolding and a default script by running the following command in the project root directory:

  • --name functionName specifies the name of the Function. Names currently must start with a letter and be all lowercase letters or numbers.
  • --language language specifies the programming language to use, such as java, javascript, or typescript

This creates a function template and any supporting build and configuration files. A new JavaScript Function has the following files created:

functions/myfunction/project.toml contains Function metadata information. See Developer Guide: Function Metadata TOML Files for more details.

functions/myfunction/package.json is used to specify any dependencies required by the function.

functions/myfunction/index.js is your primary source code file. It is pre-configured with method parameters that you can use to interact with information in an incoming request, and with data in your Salesforce org. The default generated index.js template looks like this:


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 to create an Account record, insert it, and verify the new record using a query. Populate the new Account's name using a field named name in the payload. You can copy the code from the Context_SalesforceSDK_JS sample. The code should look like the following: