Blog

Software tips, techniques, and news.

Using Lambdas to Overcome Salesforce Governor Limits

Salesforce is a great CRM product, but it has some limitations that can prove troublesome depending on the situation. One such limitation is caused by the multitenant nature of Salesforce. Salesforce hosts each and every org on their servers, which is beneficial for the customer since Salesforce handles the upkeep and maintenance. However, since each org has to share Salesforce’s processing power, Salesforce has implemented governor limits to ensure that each org gets its fair share of the resources. Such governor limits can be only 150 DML statements per call or up to 50,000 records retrieved by SOQL queries.

Salesforce is pretty generous with their governor limits, and there are best practices to ensure that processes can avoid hitting limits. However, there are still situations where a limit can be hit, such as if you’re working with large files that pass the 6MB heap limit. Luckily, there are ways around these governor limits.

youtube-preview

AWS Lambda

While Salesforce has its governor limits, some external softwares have no such limit. AWS Lambdas are a feature of Amazon Web Services to consider when circumventing the governor limits. AWS Lambdas are event-driven and serverless, which means that you can trigger them from Salesforce and you don’t have to worry about the infrastructure hosting your callouts. Overall, this means you just have to build the Salesforce callout and the AWS Lambda function to get your processes running. Another benefit of AWS Lambda is that you can build the functions using a variety of programming languages, opening up access to a plethora of libraries to work on your data.

Method Overview

There are a couple of points that have to be considered when setting up everything.

The Trigger

You have to consider what event you want to trigger the AWS Lambdas. This could be when a manual change has been made to your records, such as when a user updates a contact’s info, or from a scheduled event, such as a flow that runs on accounts every night. Consider the type of work you are trying to perform on the data, and what governor limits you may hit with it. Perhaps you need to perform a long calculation on the data that will take too long, or you need to group a lot of data that cannot all be queried at once with the governor limits. If you think you have a process that might have scaling issues, you should consider setting up an AWS Lambda for it.

salesforce governor limits with lambda trigger.

The Lambda Function

Once you know the trigger, you will have to build an AWS Lambda to perform work on the data. You will have a wide variety of programming languages to work with, so consider using what you are familiar with or what is best for the job at hand. Now that you’re in the lambda function, you can work with less stringent limitations. For instance, Salesforce has a maximum CPU time of 10 seconds on its servers for synchronous apex transactions and 1 minute of time on its servers for asynchronous apex transactions. AWS Lambda functions can run for up to 15 minutes.

salesforce governor limits with lambda aws create function.

Connect AWS to Salesforce

Once your data is ready, you will need to send it back to Salesforce. There are a couple of ways to do this. For instance, if you are using Python for your lambda function, you can install the Simple Salesforce library. Simple Salesforce offers a low-level REST API to send and receive data from your Salesforce org. Once you access your Salesforce Session in the code, you will be able to update your Salesforce records in the lambda function.

salesforce governor limits with lambda python siple salesforce library.

You can also create a Connection to your Salesforce org. You will need your org’s URL and the generated consumer secret to create this connection. There are a couple of steps that you will need to perform. First, you will need to create a Connected App in Salesforce. A connected app is a framework that allows external applications, like AWS, to communicate with Salesforce using APIs. Once you have created a Connected App, you can take the generated consumer key and consumer secret and use them in AWS to finalize the connection. This help article goes through the steps of creating a connected app to Amazon. Once that has been done, you create a connection using API destination and Event Rule in AWS. This AWS doc goes over the steps of creating those components in order to send data from AWS back to Salesforce.

salesforce governor limits with lambda connected apps.

Connect Salesforce to AWS

With everything else set up, all that is left to do is set up our Salesforce org to send the data to the lambda function. An Amazon API Gateway will have to be created for your AWS. API Gateways are how you give external systems, like Salesforce, access to data or functionality in your AWS. Once you create an API Gateway to your lambda function, you will have access to a URL that you can call in Salesforce to run your function.

salesforce governor limits with lambda amazon api gateway.

You will need to send a message containing the data you want to work with to the URL that was created for your API Gateway. If you are working with Apex, you can send the data using an HTTP callout.  For workflows, such as approval processes or flows, you can define an Outbound Message. For Outbound messages, you have to select what object data you are sending, select which fields to send, as well as the Endpoint URL. Whether it is an HTTP callout or an Outbound Message, consider what data you will need to send for the AWS Lambda to function properly.

salesforce governor limits with lambda outbound message.

Conclusion

AWS Lambdas are an alternate way of dealing with Salesforce’s governor limits. Aside from giving you access to other programming languages for your data, it also improves the scalability of your org by offloading the work to AWS. Creating the connections between Salesforce AWS may take a bit of know-how, but once that is in place, everything is quite maintainable. Contact DB Services if you have any questions or need assistance utilizing lambda functions for your Salesforce Org.

Need help with your Salesforce digital transformation? Contact us to discuss Salesforce consulting, implementation, development, and support!

jason swan headshot.
Jason Swan

Jason is a reliable developer with a systematic approach. He focuses on consistently delivering accurate and precise results and setting clear expectations. With an unwavering commitment to quality, he consistently contributes to his team's success, providing support and valuable insights that elevate each project.