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.
Create a Sandbox 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.
1{2 "sandboxName": "sandbox1",3 "licenseType": "Developer" // put your license type here4}
From the same directory, use the CLI to create a sandbox org.
--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.
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.
Note
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.
Login and Set an Alias To Your Sandbox Org
1sf org login web --alias MySandboxOrgAlias --set-default
--alias set an alias for the authenticated org
--set-default set sandbox as default org
Connect to Salesforce Functions
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.
Note
1sf login functions
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.
Create a Compute Environment
After creating the sandbox org, create a Salesforce compute environment that’s associated with that org. Your functions deploy to this compute environment.
1sf env create compute -o MySandboxOrgAlias -a MyComputeEnv # use your sandbox org alias
-o Alias of the org the compute environment is connected to
-a Alias for the newly created compute environment
Finish Writing Your Function
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.
1export default async function(event, context, logger){2 logger.info(`Invoking salesforcesdkjs function with payload ${JSON.stringify(event.data ||{})}`);34 // Extract properties from payload5 const{name, accountNumber, industry, type, website} = event.data;67 // Validate the payload params8 if(!name){9 throw new Error(`Please provide account name`);10}1112 // Define a record using the RecordForCreate type and providing the Developer Name13 const account = {14 type: "Account",15 fields:{16 Name: `${name}-${Date.now()}`,17 AccountNumber: accountNumber,18 Industry: industry,19 Type: type,20 Website: website,21},22};2324 try{25 // Insert the record using the SalesforceSDK DataApi and get the new Record Id from the result26 const{id: recordId} = await context.org.dataApi.create(account);2728 // Query Accounts using the SalesforceSDK DataApi to verify that your new Account was created.29 const soql = `SELECT Fields(STANDARD) FROM Account WHERE Id = '${recordId}'`;30 const queryResults = await context.org.dataApi.query(soql);31 return queryResults;32}catch(err){33 // Catch any DML errors and pass the throw an error with the message34 const errorMessage = `Failed to insert record. Root Cause: ${err.message}`;35 logger.error(errorMessage);36 throw new Error(errorMessage);37}38}
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.
Add Project to git
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.
Deploy Function to Compute Environment
To deploy your project’s function, use the following command with sandbox org alias we created earlier.
1sf deploy functions -o MySandboxOrgAlias
The deploy process can take several minutes.
To check the status of your deployed project, use sf env list.
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.
Update the Functions Permission Set
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:
1Deploying v59.0 metadata to test-1234dbzjb@example.com using the v60.0 SOAP API.2Deploy ID: 0Af6t00000jWn3Status: Succeeded | ████████████████████████████████████████ | 1/1 Components | Tracking: 1/145Deployed Source6========================================================================================================7| State Name Type Path8| ─────── ───────── ───────────── ──────────────────────────────────────────────────────────────────────9| Created Functions PermissionSet force-app/main/default/permissionsets/Functions.permissionset-meta.xml
For more information on syncing project and sandbox org changes, see Develop Against Any Org.
Note
Enable Source Tracking for Sandboxes
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.
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.