Configure Orgs for Functions

Salesforce Functions are deployed to the Salesforce compute environments rather than your org. Before your org can invoke a deployed Function, configure it to work with Salesforce Functions, create a compute environment, and associate it with your org.

Before your org can work with Salesforce Functions, you must enable the Salesforce Functions org setting. This org setting creates an authenticated connection for requests between your org and Salesforce Functions.

Before you can deploy a function or invoke a function from an org, you must create a compute environment. A compute environment in the Salesforce cloud represents a place where your deployed functions run. When you create a compute environment you specify an org that the compute environment is connected to. That org can then access and invoke functions deployed to that compute environment.

When developing Functions, you'll most likely be creating compute environments associated with your sandbox or scratch orgs. When you're ready to deploy the Function to production, you'll create a compute environment associated with your production org and deploy your Functions to that compute environment.

To create a compute environment, use the sf env create compute command, specifying the org, and optionally an alias that you can use after the compute environment has been created:

List Compute Environments

To check which compute environments you've created, use the sf env list command:

Delete Compute Environments

If you no longer need a compute environment, or you wish to disconnect a connected org, use the sf env delete command with the compute environment alias to remove the compute environment:

When developing your Function, use scratch orgs created from your Dev Hub org to test Salesforce data access. Scratch orgs are disposable Salesforce orgs that support development and testing. Make sure to first enable Functions in your Dev Hub org.

To create a scratch org for your Function, first edit the config/project-scratch-def.json file in your DX project. Add the “Functions” feature:

config/project-scratch-def.json

Use your updated project-scratch-def.json to create a scratch org via the sfdx force:org:create CLI command:

If your current default Dev Hub org isn't the Dev Hub that has Functions enabled, specify the Functions-enabled Dev Hub org using the -v argument. For example: sfdx force:org:create -s -f config/project-scratch-def.json -a MyScratchOrgAlias -v MyFcnsDevHubAlias

To access your scratch org (to add data records for testing, for example), use the sfdx force:org:open CLI command, which logs in to your scratch org in a browser window.

By default, scratch orgs expire after 7 days. Salesforce Functions doesn't remove compute environments for expired orgs, so you must manually delete compute environments for expired scratch orgs.

For more information, see Scratch Orgs.

Keep track of the scratch org alias you use—you’ll need it again when you invoke your Function. You also need it when you create the compute environment associated with your scratch org.