Conf42 Cloud Native 2022 - Online

Elastic Compute with Salesforce Functions

Video size:

Abstract

Salesforce Functions (https://developer.salesforce.com/docs/platform/functions/overview) help you deliver scalable experiences thanks to the power of elastic compute and open language flexibility. In this session I will explain:

  • How we’ve implemented salesforce functions making use of popular open source technologies
  • How functions are written in node or java (for now), how they can be invoked from Salesforce, and how it can interact with Salesforce data seamleassly
  • The tooling that we have created to work with Functions (a CLI to run them locally, deploy them, etc.).

Everything from a practical approach showing code / running demos.

Summary

  • Salesforce functions is a feature that we have created in Salesforce to provide functions as a service. Functions are serverless, which means that developers can build applications without having to manage infrastructure or servers. Everything runs within the Salesforce trusted infrastructure.
  • Trying out Salesforce is free. You can request an environment and to play with functions for free. Here we take a look at how to use the Salesforce CLI to deploy a salesforce function. All the code that we have seen today is public.
  • To finalize, I want to share some resources to summarize really the resources that I have shared along the talk. Discover Salesforce functions Trailhead is our tutorized learning platform. You can find us at developer salesforce. com. Follow us on Facebook, YouTube and LinkedIn.

Transcript

This transcript was autogenerated. To make changes, submit a PR.
Hi everyone, my name is Alvarivas and today I'm going to talk to you about elastic compute with Salesforce functions. I am a develop developer advocate at Salesforce and here you have my contact details in case that you want to ask me anything later or to contact me later. This is a slide that I need to include because I am a Salesforce employee, which just means that as Salesforce is a publicly traded company, you should make purchasing decisions based on the functionality that is currently available. And with that, let's start talking about functions. But what is a function? A function is a short lived executable that is invoked on demand. Functions are serverless, which means that developers can build applications without having to manage infrastructure or servers, and this reduces a lot the operational costs. Functions are also elastic, which means that they will scale, taking more resources or less resources as needed based on demand. And what are Salesforce functions? Salesforce functions is a feature that we have created in Salesforce to provide functions as a service. In case that you don't know what Salesforce is, I'm going to give you a quick introduction. So Salesforce obviously is the name of a company, but also is a name for a CRM, a CRM that offers products for managing your relationships with your customers and clients, such as an application for sales, another one for service, for customer service, another one for marketing, for commerce and for much more. But Salesforce, on top of being a CRM, is also an application development platform. If you want to build applications on top of Salesforce, you can do that two ways. You can use low code tools or you can use code. I am a Salesforce developer, so I normally use code. The technologies that you can use to build applications with code in the platform are JavaScript. For the front end we use JavaScript and web components based framework called Lightning web components. And for the backend we have historically used Apex. Apex is an object oriented language very similar to Java and it's been there from the beginning. But for the backend since November last year, we are also able to develop Salesforce functions, right? So as I mentioned before, a Salesforce function is a short lived on purpose executable that is going to be invoked. Star and Phoenix, right? They are going to be invoked and run in a serverless way in which we are not going to need to manage the infrastructure that's behind the scenes. And they are going to be elastic, which means that they are going to scale, taking more or less resources depending on the demand. What else is special about Salesforce functions? Well, first of all, Salesforce functions run within the Salesforce trusted boundary. This means that from a function you are going to be able to work with Salesforce data, Salesforce records as your accounts or your leads, or whatever data you are storing in Salesforce, you are going to be able to work with those records in your function and then you are going to be able to send that data back to Salesforce. If you wanted to do some updates or insert some new data, whatever, right? This is possible thanks to an SDK that we use within the function and everything. This is very important. Everything runs within the Salesforce trusted infrastructure. Your data never goes out from Salesforce, which means that it's going to be safe. And third, I wanted to mention the different languages in which you can write Salesforce functions, because on the platform we only had apex in the past, but now with Salesforce functions you can write that code, the code that a function will run in JavaScript and Java for now, but we are also working in supporting more languages in the future. Now I want to talk a little bit about the open source technologies that we have used to build the feature to build Salesforce functions. First of all, we are using kubernetes. Kubernetes is an open source container orchestration engine that you can use to deploy, scale and manage your applications automatically, right? When they are containerized applications. We are also using cloud native buildpacks. Cloud native buildpacks is a technology that allows you to transform your application source code into a containerized application that then can run in any cloud, in our case a Kubernetes infrastructure. And finally, I wanted to mention cloud events. Cloud events is not really a technology, it's more a specification, right? But this specification will help you standardize the way in which we describe event data, and that way we will improve and standardize how systems and servers communicate this event data. So this specification, we are following it also in Salesforce function. Now let's take a look at a demo in which I'm going to show you the anatomy of a salesforce function. So here I have a set of functions that I have created both in Java and JavaScript. Let's take a look at a function in JavaScript. This function here as it is a node function, the entry point will be the index js file. In this file, first we are reading a JSON. This JSON contains thousands of records that correspond to schools, and for each school we have the latitude and longitude. Then we have the definition of the function. A function is an async function that you define in JavaScript that receives three parameters. It receives the event parameter, context parameter and logger parameter. The event parameter contains the payload with which the function has been invoked. The context parameter is the way in which we can link back to salesforce. If we want to do a query, for instance to retrieve salesforce records, we are going to do that through the context parameter. And finally the logger parameter helps us logging. For instance, if we want to drop our logs into a log drain, we can do that by configuring the lower parameter. So what does this function do? This function receives in its payload, in the payload with which it is invoked, a latitude and a longitude. Then the function is going to compute, it's going to take a look at the JSON file and it's going to compute which are the five closest schools to the latitude and longitude provided. This is a compute intensive operation. That is the reason we decided to create a salesforce function instead of doing this calculation in apex. Finally, the function returns the calculated schools, those schools that are more closer. Let's take a look at the same function but written in Java. This is a function we can open the entry point here and as you can see the function is really similar. We need to override an apply method that receives as a parameter the event and the context, same as it happened with the Javascript function. In this case the logger is not received as a parameter because it's a log for j instance. Then in the function we can perform exactly the same calculations but written in Java. Great. So now that you know how a function looks like, let's take a look at the different ways in which you can invoked a salesforce function. Functions need to be invoked from apex. You can invoke them synchronously or asynchronously. At the moment this is the only way in which you can invoke them, but we are planning to have other entry points in the future. Then on the platform you can create a lining web component or a piece of UI with a low code tool that invokes that apex code. This is what this screenshot represents. So now let's take a look at a demo in which we will see how to invoke a salesforce function. So, same as we had our functions folder here, which is what you deploy to your compute environment, we have another folder that contains all the code that is deployed to Salesforce. In that folder you are going to find all your apex classes. So let's open up this apex class in which we are invoking the process large data function. First of all notice that apex classes are very similar to Java classes. You have classes, you have methods that can be static, that have a return value and receive a parameter. I mentioned in this so that you have an idea of the syntax of the language, and then in the body of the method you can invoke the function. If you invoke the function synchronously, what you do is that you first instantiate the function using the name of the project and the name of the function. Then you can invoked the function this way, calling the invoke method and passing in a payload. In our case, the payload will contain the latitude and longitude with which we want to compare those schools, and then we will receive a result when the invocation finishes. In the case of invoking your function asynchronously, you just move this last part to a callback method that will execute once the function finishes. Fantastic. So now let me give you some information so that you can get started with Salesforce functions. First of all, I wanted to share the link to the developer guide. You will find the documentation at SFDC co functions guide and there you will find all the prerequisites and the guides that will show you how to get started. One of the prerequisites is to install the Salesforce CLI. This is a command line tool that we use in Salesforce to manage how we deploy and retrieve metadata from our Salesforce instances that we call organizations. And we also use the Salesforce CLI to work with Salesforce functions. I also recommend you to use vs code as an ide because we have a set of extensions that we help you with Salesforce development in general. We have a portion of these extensions that is meant to help you write salesforce functions and that's why we normally use vs code with this extension installed. And also I wanted to tell you that trying out Salesforce is free and trying out Salesforce functions is also free. To ask for a developer in which functions is enabled. You can go to this URL here, functions, salesforce.com signapps and there you are going to be able to request an environment and to play with functions for free. So now let's take a look at how to use the Salesforce CLI to deploy a salesforce function. So the first thing that we need to do to deploy a salesforce function is to create a scratch or a scratch or is an ephemeral development environment that gets associated with your trial or we are creating a scratch or called functions or. Fantastic. The next thing that we need to do is to create a compute environment a compute environment is a secure salesforce managed infrastructure which is different from your salesforce.org. This is where functions are deployed to and run in. This is the infrastructure that is built with cloud native build packs and kubernetes. The deploy process uses changes tracked in git to know what to deploy. This is similar to how Heroku works. So let's commit our changes to a git repo. Great, so now we can write the Salesforce CLI command to deploy functions to our compute environment. For that we will write sf deploy functions minus o and the name of the scratcher to which you have linked your compute environment. As this is going to take a while, let's take a look at an organization which is linked to a compute environment in which I already deployed the function I created. Here some implementation examples this is aligning web component. Remember this is our framework for front end development. And from the lining web component you can invoke the function passing in a number of results that the function should return. So let's invoke the function and see the results. The function is taking my current latitude and longitude and it's returning the ten closest schools in the JSON file. Great, so if I go back to vs code we can see that the function has been correctly deployed. In this demo, I decided to deploy the function because I wanted to show you the final result when we invoke the function from the salesforce organization. However, bear in mind that you can also run functions locally. This is very useful at the time of testing and development. All the code that we have seen today is public. You can find the functions recipes sample app@GitHub.com trailheadapps functions recipes and there you will find many more sample apps that will help you get started with Salesforce development. To finalize, I want to share some resources to summarize really the resources that I have shared along the talk. First of all, you have the documentation, the developer guide. Take a look at it to get started. You also have the sign up page in which you will be able to request a trial or for free to try out Salesforce functions. Also, I leave you here again the link to the sample app that we created to help you get started with functions functions recipes and the last link is a trailhead module called Discover Salesforce functions Trailhead is our tutorized learning platform in which you are going to find more than 1000 modules to learn Salesforce in general and also Salesforce development. Finally, I want to share the different ways in which you can connect with us. You can find us at developer salesforce.com. We have there a blog, a podcast and all the documentation for developers. You can also follow us on Twitter at salesforce and you can also follow us on Facebook, YouTube and LinkedIn. We publish videos on YouTube regularly. We do live streaming and if you subscribe to the channel and click on the bell, you will receive notifications about those videos. Thank you so much and I hope that you enjoyed my talk at least a little bit. Have a good day.
...

Alba Rivas

Principal Developer Advocate @ Salesforce

Alba Rivas's LinkedIn account Alba Rivas's twitter account



Awesome tech events for

Priority access to all content

Video hallway track

Community chat

Exclusive promotions and giveaways