Salesforce

Salesforce Functions – what should you know about them?

27 maja, 2022 0
Podziel się:

There are some tasks Salesforce platform was simply not built for. There are limits to processing large data volumes or generating files. Even though these tasks are not usually necessary in CRM systems, there are still a lot of cases where apex and VisualForce are just not enough. Salesforce Functions is great for bypassing these limitations. From my observations, not a lot of Salesforce developers know the capabilities of this powerful tool. If you want to know how to make the impossible possible in Salesforce, keep reading – it might be your way to shine next time the Product Owner brings up a challenging requirement.

Salesforce functions let developers integrate with the Salesforce Platform with no complex infrastructure. The hassle you usually have to deal with when building an integration is not a concern here – you don’t have to create connected apps, integration users or allowlist IPs. Under the hood, it runs on Heroku. You get to choose the language (JavaScript, TypeScript, Java) and use open-source libraries. Anything that is possible in the language of your choice, is possible for Salesforce Functions. All you have to worry about are the Apex, API and Function Execution limits.

Salesforce Functions are deployed to, and run in, a secure Salesforce-managed infrastructure separate from your Salesforce org. You can invoke Functions directly from Apex (Invocable Apex in case of Flow).

Blank diagram 6 1 - Salesforce Functions – what should you know about them?

Use cases

You might have already thought about some of the cases when such functionality comes in handy. The possibilities are countless! I cannot create an exhaustive list of all Salesforce Functions use cases. There are so many options, even Salesforce hasn’t explored all of them yet! I did, however, list some of the most used ones below:

  • ETL Processing,
  • Heavy Asynchronous processes,
  • Generating files,
  • Using external libraries,
  • Bypassing Apex Limits.

Moving these heavy processes outside of the Salesforce platform is sometimes necessary due to technological and functional limitations, but it’s never been this straightforward before!

Setup

If you’re anything like me, you’ve probably had enough of hollow theorizing. Let’s start the engineering part, then!

First things first, before you will be able to use Salesforce Functions, you need to purchase the Functions License. Once you have it, you can start the setup process.

There are three major things to do there: You need to set up your dev environment, configure your org and set up permissions. Let me explain in more detail.

Dev Environment setup

  • Time for big decisions – choose the language you want to use! There are a few options when it comes to working with Salesforce Functions:
    • js 14 or higher to developer Functions that use JavaScript or TypeScript,
    • OpenJDK 8 or higher and Apache Maven6.3 or higher to developer Functions that use Java.

Once you are ready to make a commitment, install the language. It’s official now!

  • On top of your language of choice, you will definitely need GIT CLI. Install it if you haven’t already.
  • One more thing that will be necessary is of course the Salesforce CLI. If you have managed to read this far despite not having it installed on your device, do it now!

Org configuration

  • Enable Dev Hub in production, sandbox, or Developer Edition org.
  • Enable Salesforce Functions to create an authenticated connection for requests between your org and Salesforce Functions.
  • Go to Setup > Functions. You will see two toggles there: Enable Test Space and Enable Production Space. Be sure to switch them both on.

Setup Functions - Salesforce Functions – what should you know about them?

Set up permissions

  • For developers to work with functions, they will need certain permissions granted to them. There are a few permission sets you can assign to them. Check out the guidelines below if you’re not sure as to which are necessary:
Permission Set Name Lets the user… Required System Permissions
FunctionsAccess ●       Use Functions services and functionality.

●       Access any functionality that relies on the Heroku platform including login through CLI.

●       Deploy Functions to Scratch and Sandbox Orgs.

●       Compute Access
FunctionsProdAccess ●       Perform any actions that require access to a production space.

●       Deploy Functions-related metadata in your Salesforce Org.

●       Compute Production Access

●       Customize Application

FunctionsAdminAccess ●       Manually deploy Functions to your Production Space.

●       Manage Permission Sets.

●       Compute Production Access

●       Manage Custom Permissions

●       Manage Session Permission Set Activations

SalesforceDXAccess ●       Create and manage scratch orgs.

●       Add new Apex classes for Functions.

●       API Enabled

●       Author Apex

●       Manage Sandboxes

Development

Now that we have laid the groundwork, it’s time for the exciting part: Salesforce Functions development. There are just a few commands you need to run before we start. You will only run them the first time you start, so just in case you wanted to memorize them – don’t.

  • Log in and Set an Alias to Your Dev Hub
sf login org --alias ORG_NAME --set-default-dev-hub
  • Authenticate the CLI with Salesforce Functions
sf login functions
  • [Scratch Org only] Create your Scratch Org
sfdx force:org:create -s -f config/project-scratch-def.json -a MyScratchOrgAlias
  • Create a Compute Environment to deploy the functions and connect it to your org:
sfdx env create compute -o ORG_NAME -a COMPUTE_ENV_ALIAS

All set now! You can create your first Function.It really is way easier than you think. Just run the below commands and follow the steps. Some commands will be provided in two or three alternatives – for the terminal command line, for the VSC command line and for VSC UI buttons.

Create function

The most profound part of the process – to make the Function come into existence!

sf generate function -n functionname -l javascript
or
Ctrl + Shift + P > SFDX:Create Function

create function - Salesforce Functions – what should you know about them?

Test function locally

If you’re a skeptic, here’s how to verify it works:

  • First, run the function
    Ctrl + Shift + P > SFDX: Start Function
    or
    Sf run function start (executed from the function directory
  • Now Invoke it with a test payload
    • Create a payload.json file in your function directory
    • Navigate to your project root directory and run
sf run function -l http://localhost:8080 -p '{"accountName": “From Test”}' - if you want to pass a payload in the command
or
sf run function -l http://localhost:8080 -p '@functions/myfunction/payload.json' - if you want to pass a path to the payload
or
Open payload.json and click invoke - if you want to use UI

FunctionInvoker - Salesforce Functions – what should you know about them?

  • Finally, if you want to stop the function, just use:
Ctrl + Shift + P > SFDX: Stop Function
or
Ctrl-C

Deploy the functions

Now that you’ve tested your function locally, you’re ready to deploy it! Let’s move it to your Compute Environment.

When it comes to deploying Salesforce Functions, there’s one thing that might be different from your experience from Apex. Your local version of code will never be deployed if uncommitted, as the deploy process always looks to the changes tracked in git. Do remember to commit your changes before each deployment.

git add .
git commit -m “commit message”
sf deploy functions -o ScratchOrgAlias

 To check the status of your deployed project, use

sf env list

tabela - Salesforce Functions – what should you know about them?

Invoke function from Apex

We’re back on our Salesforce Platform, but now we have more possibilities. Run whatever you have developed in your language of choice – it’s really easy.


public with sharing class GenericFunctionInvoker {
@AuraEnabled
public static String invoke(String functionName, String payload) {
// Instantiate the function
functions.Function function = functions.Function.get(functionName);

// Invoke the function
functions.FunctionInvocation invocation = function.invoke(payload);

// Check if there is any error during the invocation
if (invocation.getStatus() == functions.FunctionInvocationStatus.ERROR) {
throw new CalloutException(
(invocation.getError() != null
? invocation.getError().getMessage()
: 'UNKNOWN')
);
}

String response = invocation.getResponse();

// Return the response to the flow
return response;
}
}

This is probably the moment when something will go wrong. How do I debug, you might ask. To view Compute Environment Logs, use this command:

sf env log tail -e COMPUTE_ENV_ALIAS

Limits

No tool has unlimited power. Even though Salesforce Functions is a heavenly tool, sky is still the limit.

There are four main things you can exceed:

  • Function run time
    Invoked functions can only run for so long before they time out. The limit is much higher than in apex, but it’s still worth being aware of:
    • 15 minutes for asynchronously invoked functions,
    • 2 minutes for synchronous calls.
  • Payload size
    Payloads of a certain size are too big for functions to accept. Here’s the numbers:
    • 12MB for asynchronously invoked function,
    • 6MB for synchronously invoked functions.
  • Response size
    • 12MB for asynchronously invoked function,
    • 6MB for synchronously invoked functions.
  • Simultaneous function invocation
    • 10 functions for synchronously invoked functions.

***

Yes, there is no limitation to simultaneous synchronous function invocations. Have we just found a hole in.

Sources

***

You can find more articles about Salesforce here: The role of the Resource Manager in the recruitment process, Salesforce B2B Commerce Lightning – a tool for building e-commerce platforms, and From a user to the abuser – how to become a Salesforce expert. 

Kategorie: Salesforce
Adrian Durlak
Autor: Adrian Durlak
Salesforce Developer ze szczególnym zamiłowaniem do integracji. Jego ulubionym aspektem pracy na technicznym stanowisku w Salesforce są momenty, gdy zgłębiane zagadnienie zaczyna układać się w jego głowie w pomysł na optymalną implementację. Prywatnie lubi sci-fi, pepsi max i Kraków, którego jest aktualnie dumnym mieszkańcem

    Imię i nazwisko (wymagane)

    Adres email (wymagane)

    Temat

    Treść wiadomości

    Zostaw komentarz