0% found this document useful (0 votes)
26 views6 pages

Govenor Limits in Apex

The document discusses Salesforce governor limits including: - The limit of 10000 DML statements per transaction is hit when trying to insert more than 10000 records - Bulkifying code by using a for loop to add records to a list and inserting once allows inserting 10000 records in a single transaction - Storage limits are also discussed, as inserting 50000 records exceeds the limits of a learning edition org - Retrieving records using LIMIT allows deleting records in batches to avoid limits - CPU time limits of 10 seconds for synchronous and 60 seconds for asynchronous code are also covered

Uploaded by

SITHARTHAN V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Govenor Limits in Apex

The document discusses Salesforce governor limits including: - The limit of 10000 DML statements per transaction is hit when trying to insert more than 10000 records - Bulkifying code by using a for loop to add records to a list and inserting once allows inserting 10000 records in a single transaction - Storage limits are also discussed, as inserting 50000 records exceeds the limits of a learning edition org - Retrieving records using LIMIT allows deleting records in batches to avoid limits - CPU time limits of 10 seconds for synchronous and 60 seconds for asynchronous code are also covered

Uploaded by

SITHARTHAN V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Play with

limitation
APEX
Try to create 50000 records in custom object testobject__c

For Single Transaction

150 DML statement


10000 DML Operation

Public static void dmloperation(){


List<TestObject__c> createRecords = new List<TestObject__c>();
for(Integer i = 0; i<=10000; i++) {

TestObject__c newRecord = new TestObject__c(Age__c = i);


createRecords.add(newRecord);
}
insert createRecords;
} Hit the governor limit

System.LimitException: Too many DML rows: 10001

Public static void dmloperation(){


List<TestObject__c> createRecords = new List<TestObject__c>();
for(Integer i = 1; i<10000; i++) {

TestObject__c newRecord = new TestObject__c(Age__c = i);


createRecords.add(newRecord);
}
insert createRecords;
}
How DML statement show 1 out of 150

A for loop is set up to iterate from 1 to 9999


(inclusive), as indicated by for(Integer i = 1; i<10000;
i++). This loop will create 9999 new instances of
TestObject__c.
Each newly created TestObject__c instance is
then added to the createRecords list using the
add method.then using dml operation insert to
insert the list createRecords

The code demonstrates a best practice in Salesforce


development known as "bulkification," which ensures that the
code properly handles large sets of data by minimizing the
number of DML operations
I sucessfully created 10000 record on testobject__C ,then I try to
create remaining 40000 records

Public static void dmloperation(){


List<TestObject__c> createRecords = new List<TestObject__c>();
for(Integer i = 10000; i<20000; i++) {

TestObject__c newRecord = new TestObject__c(Age__c = i);


createRecords.add(newRecord);
}
insert createRecords;
}

Then I check my org storage Go-setup Storage

My org data storage limit is 5.0 MB but I used 21.1 MB beacuse it is


learning edition org ,cannot able to create 50000 records
Then I try to delete my testobject__c records

Public static void dmloperation(){

List<TestObject__c> RetriveRecords = [SELECT


id FROM
TestObject__c] ;
delete RetriveRecords;

NOTE : We can fetch 50000 rows of record in single transaction

Public static void dmloperation(){

List<TestObject__c> RetriveRecords = [SELECT


id FROM
TestObject__c
LIMIT 10000] ;
delete RetriveRecords;
}
CPU TIME LIMIT
CPU time refers to the processing time consumed by Apex code
execution, triggers, and other processes that run on the Salesforce
servers. This CPU time is calculated in milliseconds and is strictly
monitored by Salesforce to ensure fair resource allocation among
all users on its multi-tenant platform

10 seconds for synchronous


60 seconds for asynchronous

for(integer i=0 ; i< 10000000; i++){


system.debug(i);
}
Due to the high number of iterations, this
operation takes more than 10 seconds to
complete, thereby exceeding the Apex
CPU time limit enforced by Salesforce on
synchronous transactions, which is 10
seconds. This results in a CPU time limit
exceeded error

Best Practices:
To avoid hitting these limits, Salesforce developers are
advised to write efficient code, optimize SOQL queries, use
appropriate data structures.
limit the use of loops where possible, and follow best
practices for bulkifying Apex code to handle large data
volumes efficiently

You might also like