About:
Salesforce Function meets business needs with on-demand, elastic scale on trusted infrastructure designed to boost developer productivity by reducing your responsibilities and can be written in standard programming languages like JavaScript, Typescript, Java and so on. It provides seamless access to data in your org and can be integrated with other low code and pro-code services. Use Apex for heavy tasks to process functions without considering the Salesforce Org Limits.
When to use Salesforce Function?
Wherever the processing time gets extended, we can use the Salesforce Function.
Salesforce Function’s use cases include:
- During the batch processing, as we are going through complex logics, we can use Salesforce Function to limit the CPU usage.
- We can use in Apex trigger, during the calculation such as quote, invoice and sales deals.
Where can we invoke?
Salesforce functions can be invoked from Apex running anywhere on the platform. This means that functions can be invoked from
- Flows
- Process Builder
- Lightning Web Components
- Apex Triggers
- Batch Jobs
How Salesforce Functions Work?
The following steps should be involved for the function development:
Use the programming language of your choice such as JavaScript, Java, or TypeScript to develop your function.
We can use our favourite development environment on local workstation using VS Code, and VS Code extensions.
1. Our Salesforce CLI will evaluate our function locally and validate using scratch or sandbox orgs.
2. Deploy your function to the Salesforce Functions infrastructure in Sandbox/production.
3. Salesforce Functions compute environments to manage our function’s application lifecycle.
4. Invoke your deployed Function in your Sandbox or production orgs.
Anatomy of Salesforce Function (JavaScript)
Each JavaScript exports an async function, where our work is done and has access to event, context and logger objects.
modle.exports = async function (event, context, logger) {
}
Event – The event contains the payloads that was passed into the function
Context – The context allows to an authenticated Org and Salesforce SDK
Logger – The Logger helps you log right from your function
Invoking function (Sync)
Functions can be invoked directly from the Apex. Use the functions.Function class for function reference to invoke it by passing a JSON payload. Then, the response will be received from the functions.FunctionInvocation object.
You can invoke synchronously and asynchronously.
Sample screen to run Salesforce SDK function:
Sample Code for creating a record using node.js (Salesforce SDK Api)
How to call above Salesforce function using Apex?
Conclusion:
Using Apex, we invoked the Salesforce function created with node.js for Creating an account in Salesforce. Thus, we can extend the customer 360 platform with custom elastically scalable business logic.
Reference:
To get stared with Salesforce Function: Get Started | Salesforce Functions | Salesforce Developers
Trailhead for function recipes: https://trailhead.salesforce.com/content/learn/projects/quick-start-functions-recipes-app