what is a function?
A single purpose,on demand,short-lived microservice without complex infrastructure.
1.Elastic Scale
Extend Salesforce data with elastic compute that scales on demand.
2.Stay on platform
Build with pre-configured secure access to C360 data.
3.Expand Talent Pool
Extend Salesforce data with elastic Build inclusive with all your developer talent
using open languages and tools that scale on demand.
Anatomy of a Function :
Each javascript Function exports an async function this is where all of the works is done.
module exports = async function(event,context,logger){
// Do something
}
Each function has access to the event,context & logger objects.
The event contains the payload that was passed into the function.
The Context allows access to an authenticated Org and the Salesforce SDK.
The logger helps you log right from your function.
Invoking Function(Sync)
Functions can be invoked directly from Apex.
you can use the functions.Function class to get a function reference
and invoke it by passing it a JSON payload.
You can then get the response from the functions.FunctionInvocation object.
You can invoke them synchronously and asynchronously
public with sharing class GenericFunctionInvoker{
public static String invoke(String functionName,String payload){
functions.Function function=functions.Function.get(functionName);
functions.FunctionInvocation invocation=function.invoke(payload);
String response=invocation.getResponse();
return response;
}
}
Invoking Functions (Async) :
An async invocation receives a callback, which inherits from functions.FunctionCallback.
public with sharing class GenericFunctionInvoker{
public static String invoke(String functionName,String payload){
functions.Function function=functions.Function.get(functionName);
functions.FunctionInvocation invocation=function.invoke(payload, new MyCallback());
}
public class MyCallback implements function.FunctionCallback{
public void handleResponse(functions.FunctionInvocation invocation){
String response=invocation.getResponse();
}
}
}
Enable Functions in scratch org :
Edit the config/project-scratch-def.json file to enable the "Functions" feature and save. We will use this configuration to create a new Scratch Org:
{
"orgName": "acme company",
"edition": "Developer",
"features": ["Functions"]
}
No comments:
Post a Comment