Develop Functions Using Scratch Orgs

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.

The preferred method of developing functions is using scratch orgs. If you usually use sandbox orgs in your workflows, see our guide on how to Develop Functions Using Sandbox Orgs instead.

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 with the sf org create scratch CLI command:

sf org create scratch --set-default --definition-file config/project-scratch-def.json --alias MyScratchOrgAlias

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 --target-dev-hub flag. For example: sf org create scratch --set-default --definition-file config/project-scratch-def.json --alias MyScratchOrgAlias --target-dev-hub MyFcnsDevHubAlias

To access your scratch org (to add data records for testing, for example), use the sf org open CLI command, which logs in to your scratch org in a new 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.