Invoke a Function From Apex

Now that your project is deployed, you can invoke your Function from Apex. Make sure the current directory is your project root directory and use the following commands to create an Apex class:

In VS Code, from the Command Palette, choose SFDX: Create Apex class and enter the filename FunctionApex.

This creates a FunctionApex.cls file with template code. Replace the contents of the file with the following Apex code:

This method gets the myfunction Function in the MyFunctionProject project deployed to the compute environment connected to your org. Next, the Function is invoked synchronously with a sample payload, and the response is printed.

Deploy your Apex code to your scratch org using the following command:

In VS Code, from the Command Palette, choose SFDX: Push Source to Default Scratch Org.

Open the scratch org’s Developer Console to create a TraceFlag record. This record enables Apex debug logs in your scratch org and lets you execute force:apex:log commands if needed.

Now, invoke your Apex code to execute your function:



The output looks something like this:

While your deployed Function is running, you can view loglines generated by your Function. For more details, see Salesforce Functions Developer Guide : Function Logging