Invoke Functions Locally

Functions can be invoked locally for development and testing purposes. Local Invocations don’t impact Functions Limits.

Run Local Invocation 

You can run and invoke your function locally using the Salesforce CLI. Before invoking locally, make sure you’ve created a scratch org with Salesforce Functions enabled, as described in Develop Functions Using Scratch Orgs. Also make sure your functions permission set is configured properly as described in Function Permissions.

You can only have one function running locally at a time.

Important

Start the Function Locally Using Salesforce CLI 

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

1sf run function start

Start the Local Function Using Container 

Alternatively build and run your function locally in a container, like Docker. Make sure your container software is running, then run:

1sf run function start container

The function is ready to receive requests when you see Starting {FunctionName}. Leave the terminal window running and use a new terminal window for the next step.

Invoke the Locally Running Function Using CLI 

To test a running function, send it a JSON payload. Pass the payload inline or create a payload.json file in your function directory:

1{
2  "name": "MyAccount",
3  "accountNumber": "123456789",
4  "industry": "Technology",
5  "type": "Prospect",
6  "website": "www.salesforce.com"
7}

Navigate to your project root directory and invoke the function:

1sf run function -l http://localhost:8080 -p '@functions/myfunction/payload.json'

On Windows, remove the single quotes:

1sf run function -l http://localhost:8080 -p @functions/myfunction/payload.json

Tip

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:

1Using defaultusername MyScratchOrgAlias login credential to initialize context
2POST http://localhost:8080... 200
3{
4  "done": true,
5  "totalSize": 1,
6  "records": [
7    {
8      "type": "Account",
9      "fields": {
10        "id": "00163000012fAD0AAM",
11        "isdeleted": false,
12        "masterrecordid": null,
13        "name": "MyAccount-1623718504016",
14
15        ...fields omitted...
16
17        "yearstarted": null,
18        "sicdesc": null,
19        "dandbcompanyid": null,
20        "operatinghoursid": null
21      }
22    }
23  ]
24}

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

1{"level":30,"time":1623718504015,"pid":1,"hostname":"39f3353de3f7",
2"invocationId":"a9c869ab-2130-42e0-9615-b342c8b97bd1","msg":"Invoking
3 Myfunction with payload {\"name\":\"MyAccount\",
4 \"accountNumber\":\"123456789\",\"industry\":\"Technology\",
5 \"type\":\"Prospect\",\"website\":\"www.salesforce.com\"}"}

To invoke the function with an inline payload:

1sf run function -l http://localhost:8080 -p '{"id": 12345}'

On Windows, escape any double-quote marks (“) in the payload with a backslash (”\“). For example:

1sf run function -l http://localhost:8080 -p '{ \"id\": 12345 }'

Have your locally running function connect to the scratch org by providing the scratch org alias with the -o argument.

You often set your scratch org alias when you create your scratch org, as described in Develop Functions Using Scratch Orgs. Visit Create a Scratch Org for help on creating and configuring your scratch org.

Tip

1sf run function -l http://localhost:8080 -p '{ "id": 12345 }' -o TestScratchOrg

Stop Function Locally 

The function continues to run and waits for requests until you stop the function. Stop the running function by pressing CTRL-C in the terminal where your function is running.

Product Retirement Announcement

Salesforce Functions is no longer available for purchase or renewal. To preserve the capabilities that Salesforce Functions provided to your org, deploy an alternative solution before your existing order term ends. See Salesforce Functions Retirement for more information on migrating your functions. Contact your Salesforce Account Executive for more information on Heroku.