Invoke a Function From Apex
When your project is deployed, invoke your function from Apex.
Make sure the current directory is your project root directory and use the following command to create an Apex class:
In VS Code from the Command Palette, choose SFDX: Create Apex class and enter the filename FunctionApex.
This command creates a
FunctionApex.cls file with template code. Replace the contents of the file with the following Apex code:
This class file invokes the function with a sample payload and prints the response.
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. There is no command for pushing to a Salesforce org.
Debug Apex Code Deploy
Deploying your Apex code with
sfdx force:source:push can cause the following error:
This error appears when the Apex class is running on API version 52.0 or earlier. To fix this issue, navigate to
/force-app/main/default/classes/ and open the
FunctionController.cls-meta.xml file. Edit the file to update the API version to 53.0:
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:
See Function Logging to learn how to view logs while the function is running.