Develop Functions Using Sandbox Orgs

You can deploy functions to sandbox orgs for development or testing. Sandbox orgs provide a shared environment for your team to do collaborative Salesforce Functions development and testing.

To create a sandbox org, you can either create it from your DevHub org UI or using the Salesforce CLI.

To create a sandbox using the Salesforce UI, see Create a Sandbox.

When creating a sandbox using the Salesforce CLI, first create a sandbox definition file in your project's config directory, and then use sf org create sandbox to create the org.

Before you create your sandbox using the Salesforce CLI, create a sandbox definition file in your project's directory: config/developer-sandbox-def.json. The sandbox definition file is a blueprint for the sandbox.

From the same directory, use the CLI to create a sandbox org.

  • --definition-file path to sandbox config file
  • --alias set an alias for your sandbox
  • --target-org name of the production org that contains your sandbox licenses

This command starts the sandbox creation process. The process times out after six minutes, but the sandbox org will remain in the creation queue. Make sure the org is fully created in your org's Setup section before you continue with this guide.

For more information, see Create a Sandbox Definition File and Create, Clone, or Delete a Sandbox.

After creating your sandbox org, you can confirm it's set up for Salesforce Functions use by logging into your newly created sandbox org and checking the information under Setup > Functions, which should match the Salesforce Functions connection information from your DevHub org.

The Salesforce Functions project you created in the previous step doesn't automatically create a definition file, so you have to create it yourself.

A sandbox org is a copy of your production org intended for development. Use the Apex Developer Guide: Sandbox Orgs documentation for more setup instructions.

  • --alias set an alias for the authenticated org
  • --set-default set sandbox as default org

Log in to Salesforce Functions using the CLI.

To log in to Salesforce Functions while using sandbox orgs, log in to the associated production org.

The command sf login functions opens a browser page where you can log in to your Salesforce Functions account. Use the same credentials you used to connect your sandbox's associated production org.

After creating the sandbox org, create a Salesforce compute environment that's associated with that org. Your functions deploy to this compute environment.

  • -o Alias of the org the compute environment is connected to
  • -a Alias for the newly created compute environment

Edit index.js and update your function to match the following example. This function uses the Salesforce Functions SDK for Node.js to insert a new Account record in your scratch or sandbox org. It then queries all Account records with the given fields in the org. The new Account's name is populated from the name field of the payload.

Now that you’ve developed and tested your function locally, deploy your project so you can invoke your function from your Salesforce org. The first time a function project is deployed, the upload can be 500 MB or more.

Before you deploy, commit your functions code changes to a git repo. The deploy process uses changes tracked in git to know what to deploy. Since you just created this project, add the project to a new repo.

From the root directory of your project, use the following git commands:

At this point you can optionally push your changes to github.com, but it isn't required for deploying functions.

Your project is now ready to deploy. If you make additional changes to your function code, use git add and git commit to commit those changes to the repo before deploying again.

For more details on adding your repo to github.com, see Create a repo.

To deploy your project's function, use the following command with sandbox org alias we created earlier.

The deploy process can take several minutes.

To check the status of your deployed project, use sf env list.

Outputs:

For more details on deploying projects, see the Salesforce Functions Developer Guide.

Update a permission set to ensure that the function we create can access data in our Salesforce org. A permission set is a collection of settings and permissions that give users and apps access to various tools and data in Salesforce.

When you connect your org to Salesforce Functions via the Functions Setup page, Salesforce creates a Functions permission set with minimal permissions. Update that permission set to give your function access to the Account object.

To update the functions permission set, add the file force-app/main/default/permissionsets/Functions.permissionset-meta.xml, with contents:

These permissions allow a running function in a compute environment connected to your org to access the Account object.

Use project deploy start with the --ignore-conflicts flag to deploy your changes to the functions permission set to your scratch org:

Outputs:

For more information on syncing project and sandbox org changes, see Develop Against Any Org.

When developing functions with multiple contributors, sync your team's changes by using Enable Source Tracking in Developer and Developer Pro Sandboxes. See Enable Source Tracking in Sandboxes for more details.

Follow Invoke Functions with Apex for next steps on invoking your function.