- Details
-
Created: November 21 2022
-
Hits: 768
Governor limits in Salesforce are predefined limits that are set to ensure that the Apex code and Visualforce pages are executed in a secure and efficient manner. Here are 10 examples of governor limits in Salesforce and sample code to illustrate how they work:
- Maximum CPU time limit: This limit ensures that a single Apex transaction does not consume too much CPU time.
// Example code that exceeds the maximum CPU time limit
List<Account> accounts = [SELECT Id, Name FROM Account];
for (Account acc : accounts) {
for (Contact con : acc.Contacts) {
// Perform some complex operation
}
}
- Maximum heap size limit: This limit ensures that the amount of memory used by Apex code does not exceed a certain limit.
// Example code that exceeds the maximum heap size limit
List<Account> accounts = [SELECT Id, Name FROM Account];
Map<Id, Contact> contactsMap = new Map<Id, Contact>();
for (Account acc : accounts) {
for (Contact con : acc.Contacts) {
contactsMap.put(con.Id, con);
}
}
- Maximum number of SOQL queries limit: This limit ensures that a single Apex transaction does not execute too many SOQL queries.
// Example code that exceeds the maximum number of SOQL queries limit
List<Account> accounts = [SELECT Id, Name FROM Account];
for (Account acc : accounts) {
List<Contact> contacts = [SELECT Id, Name FROM Contact WHERE AccountId = :acc.Id];
// Perform some operation on the contacts
}
- Maximum number of DML statements limit: This limit ensures that a single Apex transaction does not execute too many DML statements.
// Example code that exceeds the maximum number of DML statements limit
List<Account> accounts = [SELECT Id, Name FROM Account];
for (Account acc : accounts) {
acc.Name = 'New Name';
update acc;
}
- Maximum number of callouts limit: This limit ensures that a single Apex transaction does not execute too many external callouts.
// Example code that exceeds the maximum number of callouts limit
HttpRequest req = new HttpRequest();
req.setEndpoint('https://example.com');
req.setMethod('GET');
Http http = new Http();
for (Integer i = 0; i < 100; i++) {
http.send(req);
}
- Maximum number of future calls limit: This limit ensures that a single Apex transaction does not execute too many future calls.
// Example code that exceeds the maximum number of future calls limit
for (Integer i = 0; i < 101; i++) {
Test.callout(i);
}
@future(callout=true)
public static void callout(Integer i) {
// Perform some callout operation
}
- Maximum number of batch jobs limit: This limit ensures that a single Apex transaction does not execute too many batch jobs.
// Example code that exceeds the maximum number of batch jobs limit
Database.executeBatch(new MyBatchJob());
Database.executeBatch(new MyBatchJob());
Database.executeBatch(new MyBatchJob());
- Maximum number of scheduled Apex jobs limit: This limit ensures that a single Apex transaction does not schedule too many Apex jobs.
// Example code that exceeds the maximum number of scheduled Apex jobs limit
for (Integer i = 0; i < 101; i++) {
String jobId = System.schedule('Job' + i, '0 0 * * * ?', new MyScheduledJob());
}