Skip to main content

Code reviews rule: Apex Use of Salesforce Function

Written by David Martin
Updated yesterday

Apex Use of Salesforce Function

Why is this an issue?

The Function.invoke() method was used to call Salesforce Functions, a feature that has been retired. Salesforce Functions allowed you to run custom code in elastic compute environments, but this service was discontinued.

Code that calls Function.invoke() will fail at runtime since the underlying service no longer exists. You need to migrate this functionality to an alternative approach.

Examples

Example of incorrect code (synchronous invocation):

public class OrderProcessor {
public void processOrder(Order__c order) {
Map<String, Object> payload = new Map<String, Object>();
payload.put('orderId', order.Id);

// Salesforce Functions has been retired
functions.Function orderFunction = functions.Function.get('MyProject.ProcessOrder');
functions.FunctionInvocation invocation = orderFunction.invoke(JSON.serialize(payload));
}
}

Example of incorrect code (asynchronous invocation):

public class OrderProcessor {
public void processOrder(Order__c order) {
Map<String, Object> payload = new Map<String, Object>();
payload.put('orderId', order.Id);

// Salesforce Functions has been retired
functions.Function orderFunction = functions.Function.get('MyProject.ProcessOrder');
functions.FunctionInvocation invocation = orderFunction.invoke(JSON.serialize(payload), new OrderCallback());
}
}

Example of correct code:

public class OrderProcessor {
public void processOrder(Order__c order) {
// Use Queueable Apex for async processing
System.enqueueJob(new OrderProcessorQueueable(order.Id));
}
}

How can I fix violations?

Migrate away from Salesforce Functions to one of these alternatives:

  1. Queueable Apex: For async processing within Salesforce

  2. Platform Events: For event-driven architecture

  3. External Services: For calling external APIs

  4. Heroku or AWS Lambda: For custom compute needs outside Salesforce

Resources

Did this answer your question?