Use Java Functions

If you're developing your Function using Java, here are some considerations to keep in mind.

Salesforce provides the Salesforce SDK for Java Functions specifically for Functions written in Java. This SDK provides classes to securely and efficiently access Salesforce org resources.

Full reference documentation for the Salesforce SDK for Java Functions is here: Salesforce SDK for Java Functions.

Your implementation of SalesforceFunction can use the following types for T (Function input) and R (Function output):

  • byte[] for receiving and returning raw data. Use this type if any of the following types don't fit your use case. You will need to handle any conversion of the byte array to and from more specific data formats.
  • String for receiving and returning UTF-8 encoded strings.
  • Any plain old Java object. Use this type if you plan to use a simple data schema for your Function input or output. For example, if your Function needs to accept JSON input like:

Your Java object might look like:

When your Java Function runs, Salesforce Functions will handle converting the JSON data into your Java object.

  • com.fasterxml.jackson.databind.JsonNode for accessing and returning raw JSON, parsed with Jackson.
  • for accessing and returning raw JSON, parsed with Google Gson.

If you're using VS Code, you'll need to install the Java Extension Pack to get full Java support in VS Code, including the ability to debug Java Functions. For installing the extension, see Java Extension Pack. For information on configuring and using the extension, see Getting Started with Java in VS Code.

Note that the extension also provides a way to install the JDK, if you haven't already done so. See Configure JDK for more details.

For debugging Java Functions in VS Code, add a "java" configuration to your VS Code's launch.json file. Your launch.json should look something like this:

Then, in VS Code Run view, use the newly added "Debug (Attach)" launch configuration to start and debug your Function. To provide a payload, you can start your Function in VS Code terminal specifying the port and payload you want to use, for example:

Use the Salesforce CLI to set environment variables in your Functions compute environments, and then access them in your Java Function code as regular system variables. See Environment Variables for more details.

Use the SLF4J logging framework for logging, as described in Generate Log Information in Your Function Code.

You're free to use whatever Java test frameworks you need. Some frameworks to consider include:

  • JUnit for creating tests and test harnesses. Note that version 5 is not required -- earlier versions of JUnit should work as well.
  • Mokito for creating mocks.