Bypass Salesforce Governor Limits when Working with Information


Salesforce is a multi-tenant system. This implies Salesforce assets are shared by all Salesforce accounts. To forestall the system from abuse and to take care of good efficiency throughout all Salesforce accounts, Salesforce applies numerous limits on accounts & transactions. These are referred to as governor limits.

Salesforce mentions all the governor limits right here. Though there are numerous limits, solely a few of them turn into restrictive when working with recordsdata in apex.

On this article, we discover what are the necessary file-related governor limits, why they will turn into restrictive and eventually the best way to bypass them utilizing slightly code in apex (or outdoors of it).

File Use-cases & Governor Limits

Information are a vital a part of the gross sales course of. As a Salesforce developer, you’ll typically come throughout use circumstances that contain working with recordsdata, primarily importing recordsdata from Salesforce to cloud drives corresponding to Sharepoint, Google Drive, OneDrive, Field, Dropbox, AWS S3 and so forth…

Listed below are some frequent examples of when such file uploads may be wanted when working in apex:

  1. When somebody uploads a file into chatter feed or the notes and attachments part
  2. When a file is available in as an e mail attachment
  3. When neighborhood customers add recordsdata from their portals
  4. When gross sales/service groups must add recordsdata from a lightning internet element

dont miss out iconDo not forget to take a look at: Batch Apex in Salesforce (Basics, Governor Limits, Custom Iterable of Batch)

Salesforce has a bunch of normal objects that retailer binary knowledge (blob) and are used for file administration.

There are 6 primary objects which can be used to satisfy all of the file administration wants. These are AttachmentContentDocumentContentNoteDocumentFolder & Note. The “Information” Object that’s generally utilized by gross sales & service cloud customers makes use of the ContentDocument object.

If you work with these objects in apex, you’ll typically run into governor limits like heap measurement & callout timeouts. Listed below are some frequent governor limits you’ll run into –

  1. Most heap measurement – It’s the quantity of reminiscence that’s utilized by an apex transaction. For synchronous strategies, it’s 6 MB and for asynchronous strategies, it’s 12 MB. This implies, that in case you learn a file as a blob in apex, you possibly can solely course of the recordsdata with sizes lower than these limits
  2. Most cumulative timeout for all callouts – It’s the time that each one HTTP calls would take to complete an apex transaction. This restrict is 120 seconds. Which means that in case you’re importing/downloading the file to/from an exterior service, it wants to complete inside 2 minutes
  3. Most execution time for every Apex transaction –  This restrict is 10 minutes for each synchronous & asynchronous apex strategies. In case you’re doing long-running duties like changing a file to pdf, it wants to complete inside 10 minutes

The way to bypass the Governor Limits

One technique to work across the governor limits whereas importing recordsdata to exterior storage from Salesforce is by utilizing the SObject Blob Retrieve API. This can be a REST API which supplies binary content material of any Salesforce object that has a blob area.

This technique requires an exterior service that makes an authenticated name to the blob retrieve API to fetch the binary knowledge and put it on exterior storage. Since REST API doesn’t impose a response measurement restrict & timeouts, this efficiently works across the file measurement restrict.

Right here is an instance of the best way to add a pdf attachment (ContentDocument in lightning) to AWS S3 utilizing javascript.

import axios from 'axios';
import aws from 'aws-sdk';
const { knowledge } = await axios.get(
        headers: {
            Authorization: `Bearer ${salesforceAccessToken}`
const s3 = new aws.S3({
    accessKeyId: AWS_ACCESS_KEY_ID,
    secretAccessKey: course of.env.AWS_ACCESS_KEY_SECRET,
    Key: 'check.pdf',
    Physique: knowledge,
    ContentType: 'utility/pdf',

One catch with this method is that this code must run outdoors of Salesforce utilizing an API server or cloud features like Salesforce features, AWS lambda or Azure features.

Importing to Different Cloud Storage Suppliers

The identical method can be utilized to add the recordsdata to Google Drive, OneDrive, Sharepoint, Field, Dropbox utilizing the suitable add API and supplying the precise authentication.

We advocate utilizing OAuth2 for authentication

Thus a easy mixture of a cloud operate and an authentication mechanism may help you bypass governor limits and switch any sort of recordsdata between Salesforce storage.

There are third celebration instruments that make this extraordinarily simple.

dont miss out iconTry an incredible Salesforce video tutorial right here: Governor Limits in Salesforce | Video Tutorial

Add Utilizing a third Occasion Instrument

You need to use third celebration instruments from AppExchange that make working with recordsdata simple. One such software is CloudFiles. Utilizing CloudFiles, you possibly can keep away from establishing an API server, establishing authentications or constructing integrations with totally different cloud storage platforms.

The identical performance described above might be achieved utilizing CloudFiles with one line of Apex code:

    recordsdata, // an inventory of sobjects with blob area
    'rTmK2YQnxaEIFkU_C18a62b2xWyDB', // folder id in exterior storage
    'google', // vacation spot: google|onedrive|sharepoint|field|dropbox|cloudfiles(s3)
    'mydrive', // drive id in exterior storage(elective)
    null // website id in case of sharepoint(elective for different storages)

Customers can authenticate Salesforce & vacation spot file storage utilizing the CloudFiles UI. It’s one time, one-click setup course of.

You need to use the add code as a part of flows or apex triggers to automate file administration in Salesforce. All this with out ever hitting the governor limits!

Blissful coding!

Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!