Test a Function Locally

Test functions in your local environment before you deploy to a compute environment. Build and run your function with the Salesforce CLI and invoke it locally.

Previously, local Salesforce Functions development required using a container like Docker. Containers are no longer required to run a function locally. Learn more about Local Function Invocations.

Navigate to the function directory and build and run your function:

The function is ready to receive requests when you see Starting myfunction. The function continues to run and waits for requests until you use Ctrl-C to stop. Leave the terminal window running and open a new terminal window for the next step.

In VS Code, open index.js. From the Command Palette, select Run SFDX: Start Function. Or, right-click on index.js in the Explorer and select Start Function.

To test a running function, send it a JSON payload. Create a payload.json file in your function directory:

Navigate to your project root directory and invoke the function, which is now running on your localhost:

  • -l running function URL
  • -p the payload to send to the function

This command invokes the function running on localhost port 8080 (the default for locally running functions) with the payload JSON file. The output looks something like:

The execution logs stream in the terminal running the function (where you ran sf run function start). The output looks something like this:

The Salesforce VS Code extension recognizes and automatically adds Invoke and Debug Invoke actions to the payload.json file.

This screenshot shows a sample payload.json with the Invoke and Debug Invoke CodeLens:

Invoke and Debug commands above a JSON payload in VS Code.

For details about using the VS Code debugger, see Debugging Functions.

To stop the function, enter Ctrl-C in the terminal window where you ran sf run function start.

In VS Code, select SFDX: Stop Function from the command palette to kill the local process.