Test a Function Locally

You can test Functions in your local environment before deploying to your Compute Environment. Build and run the function with Salesforce CLI and invoke it locally.

Run the Function

From your Function's directory, build and run the Function.

This command builds and runs the function. The function is ready to receive requests when you see Starting {FunctionName}.

The Function continues to run and waits for requests until you use Ctrl-C to stop. Leave the terminal window running and use a new terminal window for the next step.

In VS Code, with index.js open, from the Command Palette, choose Run SFDX: Start Function. Or, right-click index.js and select Start Function.

Invoke the Function

To test a running function, send it a JSON payload. Create a file, functions/myfunction/payload.json, for the payload:

From your DX project root directory, invoke the function:

Salesforce VS Code extension automatically adds Invoke and Debug Invoke actions to the file because it recognizes it as a format used to send test events to the Function. You can now either invoke or debug to add an Account record.

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

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

This command invokes the Function running on localhost port 8080 (the default for locally running Functions), with the payload JSON file. You see output similar to:

The execution logs stream in the terminal running the Function. The output looks something like this:

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

Stop the Function

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

In VS Code, from the Command Palette, select SFDX: Stop Function, which kills the local process.