Use Java Functions

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

Use the Salesforce SDK for Java Functions 

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.

Use SalesforceFunction Input and Output Types 

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:
1{
2  "fieldOne": "Hello World!",
3  "fieldTwo": 23
4}

Your Java object might look like:

1public class FunctionInput {
2    private String fieldOne;
3    private int fieldTwo;
4
5    public String getFieldOne() {
6        return fieldOne;
7    }
8
9    public int getFieldTwo() {
10        return fieldTwo;
11    }
12}

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.
  • com.google.gson.JsonElement for accessing and returning raw JSON, parsed with Google Gson.

Use the Java Extension Pack for VS Code 

If you’re using VS Code, 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.

Utilize Debugging 

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:

1{
2  // Use IntelliSense to learn about possible attributes.
3  // Hover to view descriptions of existing attributes.
4  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5  "version": "0.2.0",
6  "configurations": [
7    {
8      "type": "java",
9      "name": "Debug (Attach)",
10      "request": "attach",
11      "hostName": "127.0.0.1",
12      "port": 9229
13    }
14  ]
15}

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:

1sf run function --function-url http://localhost:8080 --payload '{"id": 12345}'

Set and Access Environment Variables 

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.

Utilize Logs 

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

Test Considerations 

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.

Product Retirement Announcement

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.