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:
Use your updated
project-scratch-def.json to create a scratch org via the
sfdx force:org:create CLI command:
sfdx force:org:create -s -f config/project-scratch-def.json -a 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
-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.