Invoke a Function From Apex

Now that your project and Function 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 a new Apex class:

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:

Open the scratch org’s Developer Console to create a TraceFlag record. This 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:

Linux/OSX

Windows

The output looks like this:

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