Introduction
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:
- When somebody uploads a file into chatter feed or the notes and attachments part
- When a file is available in as an e mail attachment
- When neighborhood customers add recordsdata from their portals
- When gross sales/service groups must add recordsdata from a lightning internet element
Do 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 Attachment, ContentDocument, ContentNote, Document, Folder & 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 –
- 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
- 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
- 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( 'https://ap5.salesforce.com/companies/knowledge/v55.0/sobjects/ContentVersion/0696D000001Pmk5QAC/VersionData',{ headers: { Authorization: `Bearer ${salesforceAccessToken}` }, } ); const s3 = new aws.S3({ accessKeyId: AWS_ACCESS_KEY_ID, secretAccessKey: course of.env.AWS_ACCESS_KEY_SECRET, }); s3.add({ Bucket: 'UPLOAD_BUCKET_NAME', 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.
Try 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:
cldfs.Consumer.add( 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!