You need to sign in to do that
Don't have an account?
Jeff Jobs
Understanding Governor Limits
Hi,
The purpose of this post is to confirm my understanding of Governor Limits. In the code below, let's assume that someone will use a Data Loader to mass update 200 records. I'd like to confirm:
1. The 2 SOQL Statements will only be run once for all 200 records (thus using 2 of an available 100)
2. The 2 DML Statements will only be run once for all 200 records (thus using 2 of an available 200 [some say 150, would like to know which])
3. That this code will support data loader updating for up to 200 records at a time via Trigger.new
Thanks in advance for your comments and time :)
Code:
trigger PropertyLedgerBouncedTrigger on Property_Ledger__c (before update) {
//Variable Declarations
RecordType actualFee;
RecordType expectedFee;
Decimal collectionAmount;
Date collectionDate;
String fiscalYear;
Id opportunity = NULL;
Id account = NULL;
List<Property_Ledger__c> toInsert = new List<Property_Ledger__c>();
List<Property_Fee_Reconciler__c> alsoInsert = new List<Property_Fee_Reconciler__c>();
//Get the Property Ledger RecordType Ids
actualFee = [SELECT Id FROM RecordType WHERE SObjectType = 'Property_Ledger__c' AND DeveloperName = 'Property_Actual_Fees_Ledger'];
expectedFee = [SELECT Id FROM RecordType WHERE SObjectType = 'Property_Ledger__c' AND DeveloperName = 'Property_Expected_Fees_Ledger'];
//Iterate over the records in the trigger
for(Property_Ledger__c pl1 :Trigger.new) {
//If the Bounced Date Dump field and the Amount Dump field are filled in from the Excel Data Loader...
if(pl1.Bounced_Date_Dump__c != NULL && pl1.Amount_Dump__c < 0) {
collectionAmount = pl1.Amount_Dump__c;
collectionDate = Date.newInstance(pl1.Bounced_Date_Dump__c.Year(), pl1.Bounced_Date_Dump__c.Month()+1, 25);
//Account for December bounces
if(pl1.Bounced_Date_Dump__c.Month() == 12) {
collectionDate = Date.newInstance(pl1.Bounced_Date_Dump__c.Year()+1, 1, 25);
}
//Reset the Bounced Date Dump field
pl1.Bounced_Date_Dump__c = NULL;
//Set the Fiscal Year
fiscalYear = String.valueOf(collectionDate.Year());
//Avoid Null Pointer Errors
if(pl1.Opportunity__c != NULL) {
opportunity = pl1.Opportunity__c;
}
if(pl1.Account__c != NULL) {
account = pl1.Account__c;
}
//Begin Amount/Date update logic
if(collectionDate.Month() == 1) {
pl1.January_Amount__c = 0;
pl1.January_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 2) {
pl1.February_Amount__c = 0;
pl1.February_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 3) {
pl1.March_Amount__c = 0;
pl1.March_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 4) {
pl1.April_Amount__c = 0;
pl1.April_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 5) {
pl1.May_Amount__c = 0;
pl1.May_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 6) {
pl1.June_Amount__c = 0;
pl1.June_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 7) {
pl1.July_Amount__c = 0;
pl1.July_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 8) {
pl1.August_Amount__c = 0;
pl1.August_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 9) {
pl1.September_Amount__c = 0;
pl1.September_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 10) {
pl1.October_Amount__c = 0;
pl1.October_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 11) {
pl1.November_Amount__c = 0;
pl1.November_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 12) {
pl1.December_Amount__c = 0;
pl1.December_Payout_Date__c = NULL;
}
//Create Fee Records and add to the toInsert List
Property_Ledger__c actualFeeLedger = new Property_Ledger__c();
actualFeeLedger.RecordTypeId = actualFee.Id;
actualFeeLedger.Name = 'Actual Property Ledger: Late Rent Fee';
actualFeeLedger.Property__c = pl1.Property__c;
actualFeeLedger.Opportunity__c = opportunity;
actualFeeLedger.Account__c = account;
actualFeeLedger.Fiscal_Year__c = fiscalYear;
actualFeeLedger.Description__c = 'Actual Late Fee';
toInsert.add(actualFeeLedger);
Property_Ledger__c expectedFeeLedger = new Property_Ledger__c();
expectedFeeLedger.RecordTypeId = expectedFee.Id;
expectedFeeLedger.Name = 'Expected Property Ledger: Late Rent Fee';
expectedFeeLedger.Property__c = pl1.Property__c;
expectedFeeLedger.Opportunity__c = opportunity;
expectedFeeLedger.Account__c = account;
expectedFeeLedger.Fiscal_Year__c = fiscalYear;
expectedFeeLedger.Fee_Amount__c = 50.00;
expectedFeeLedger.Collection_Date__c = collectionDate;
expectedFeeLedger.Description__c = 'Expected Late Rent Fee';
toInsert.add(expectedFeeLedger);
Property_Fee_Reconciler__c feesReconciler = new Property_Fee_Reconciler__c();
feesReconciler.Expected_Fee__c = expectedFeeLedger.Id;
feesReconciler.Actual_Fee__c = actualFeeLedger.Id;
alsoInsert.add(feesReconciler);
Property_Ledger__c actualLateRentLedger = new Property_Ledger__c();
actualLateRentLedger.RecordTypeId = actualFee.Id;
actualLateRentLedger.Name = 'Actual Property Ledger: Delinquent Rent';
actualLateRentLedger.Property__c = pl1.Property__c;
actualLateRentLedger.Opportunity__c = opportunity;
actualLateRentLedger.Account__c = account;
actualLateRentLedger.Fiscal_Year__c = fiscalYear;
actualLateRentLedger.Description__c = 'Actual Delinquent Rent Collected';
toInsert.add(actualLateRentLedger);
Property_Ledger__c expectedLateRentLedger = new Property_Ledger__c();
expectedLateRentLedger.RecordTypeId = expectedFee.Id;
expectedLateRentLedger.Name = 'Expected Property Ledger: Delinquent Rent';
expectedLateRentLedger.Property__c = pl1.Property__c;
expectedLateRentLedger.Opportunity__c = opportunity;
expectedLateRentLedger.Account__c = account;
expectedLateRentLedger.Fiscal_Year__c = fiscalYear;
expectedLateRentLedger.Fee_Amount__c = collectionAmount;
expectedLateRentLedger.Collection_Date__c = collectionDate;
expectedLateRentLedger.Description__c = 'Expected Delinquent Rent Collected';
toInsert.add(expectedLateRentLedger);
Property_Fee_Reconciler__c rentReconciler = new Property_Fee_Reconciler__c();
rentReconciler.Expected_Fee__c = expectedLateRentLedger.Id;
rentReconciler.Actual_Fee__c = actualLateRentLedger.Id;
alsoInsert.add(rentReconciler);
}
}
//If there are records in the toInsert List, insert them and the alsoInsert List into the database
if(toInsert.size() > 0) {
insert toInsert;
insert alsoInsert;
}
}
The purpose of this post is to confirm my understanding of Governor Limits. In the code below, let's assume that someone will use a Data Loader to mass update 200 records. I'd like to confirm:
1. The 2 SOQL Statements will only be run once for all 200 records (thus using 2 of an available 100)
2. The 2 DML Statements will only be run once for all 200 records (thus using 2 of an available 200 [some say 150, would like to know which])
3. That this code will support data loader updating for up to 200 records at a time via Trigger.new
Thanks in advance for your comments and time :)
Code:
trigger PropertyLedgerBouncedTrigger on Property_Ledger__c (before update) {
//Variable Declarations
RecordType actualFee;
RecordType expectedFee;
Decimal collectionAmount;
Date collectionDate;
String fiscalYear;
Id opportunity = NULL;
Id account = NULL;
List<Property_Ledger__c> toInsert = new List<Property_Ledger__c>();
List<Property_Fee_Reconciler__c> alsoInsert = new List<Property_Fee_Reconciler__c>();
//Get the Property Ledger RecordType Ids
actualFee = [SELECT Id FROM RecordType WHERE SObjectType = 'Property_Ledger__c' AND DeveloperName = 'Property_Actual_Fees_Ledger'];
expectedFee = [SELECT Id FROM RecordType WHERE SObjectType = 'Property_Ledger__c' AND DeveloperName = 'Property_Expected_Fees_Ledger'];
//Iterate over the records in the trigger
for(Property_Ledger__c pl1 :Trigger.new) {
//If the Bounced Date Dump field and the Amount Dump field are filled in from the Excel Data Loader...
if(pl1.Bounced_Date_Dump__c != NULL && pl1.Amount_Dump__c < 0) {
collectionAmount = pl1.Amount_Dump__c;
collectionDate = Date.newInstance(pl1.Bounced_Date_Dump__c.Year(), pl1.Bounced_Date_Dump__c.Month()+1, 25);
//Account for December bounces
if(pl1.Bounced_Date_Dump__c.Month() == 12) {
collectionDate = Date.newInstance(pl1.Bounced_Date_Dump__c.Year()+1, 1, 25);
}
//Reset the Bounced Date Dump field
pl1.Bounced_Date_Dump__c = NULL;
//Set the Fiscal Year
fiscalYear = String.valueOf(collectionDate.Year());
//Avoid Null Pointer Errors
if(pl1.Opportunity__c != NULL) {
opportunity = pl1.Opportunity__c;
}
if(pl1.Account__c != NULL) {
account = pl1.Account__c;
}
//Begin Amount/Date update logic
if(collectionDate.Month() == 1) {
pl1.January_Amount__c = 0;
pl1.January_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 2) {
pl1.February_Amount__c = 0;
pl1.February_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 3) {
pl1.March_Amount__c = 0;
pl1.March_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 4) {
pl1.April_Amount__c = 0;
pl1.April_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 5) {
pl1.May_Amount__c = 0;
pl1.May_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 6) {
pl1.June_Amount__c = 0;
pl1.June_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 7) {
pl1.July_Amount__c = 0;
pl1.July_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 8) {
pl1.August_Amount__c = 0;
pl1.August_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 9) {
pl1.September_Amount__c = 0;
pl1.September_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 10) {
pl1.October_Amount__c = 0;
pl1.October_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 11) {
pl1.November_Amount__c = 0;
pl1.November_Payout_Date__c = NULL;
}
if(collectionDate.Month() == 12) {
pl1.December_Amount__c = 0;
pl1.December_Payout_Date__c = NULL;
}
//Create Fee Records and add to the toInsert List
Property_Ledger__c actualFeeLedger = new Property_Ledger__c();
actualFeeLedger.RecordTypeId = actualFee.Id;
actualFeeLedger.Name = 'Actual Property Ledger: Late Rent Fee';
actualFeeLedger.Property__c = pl1.Property__c;
actualFeeLedger.Opportunity__c = opportunity;
actualFeeLedger.Account__c = account;
actualFeeLedger.Fiscal_Year__c = fiscalYear;
actualFeeLedger.Description__c = 'Actual Late Fee';
toInsert.add(actualFeeLedger);
Property_Ledger__c expectedFeeLedger = new Property_Ledger__c();
expectedFeeLedger.RecordTypeId = expectedFee.Id;
expectedFeeLedger.Name = 'Expected Property Ledger: Late Rent Fee';
expectedFeeLedger.Property__c = pl1.Property__c;
expectedFeeLedger.Opportunity__c = opportunity;
expectedFeeLedger.Account__c = account;
expectedFeeLedger.Fiscal_Year__c = fiscalYear;
expectedFeeLedger.Fee_Amount__c = 50.00;
expectedFeeLedger.Collection_Date__c = collectionDate;
expectedFeeLedger.Description__c = 'Expected Late Rent Fee';
toInsert.add(expectedFeeLedger);
Property_Fee_Reconciler__c feesReconciler = new Property_Fee_Reconciler__c();
feesReconciler.Expected_Fee__c = expectedFeeLedger.Id;
feesReconciler.Actual_Fee__c = actualFeeLedger.Id;
alsoInsert.add(feesReconciler);
Property_Ledger__c actualLateRentLedger = new Property_Ledger__c();
actualLateRentLedger.RecordTypeId = actualFee.Id;
actualLateRentLedger.Name = 'Actual Property Ledger: Delinquent Rent';
actualLateRentLedger.Property__c = pl1.Property__c;
actualLateRentLedger.Opportunity__c = opportunity;
actualLateRentLedger.Account__c = account;
actualLateRentLedger.Fiscal_Year__c = fiscalYear;
actualLateRentLedger.Description__c = 'Actual Delinquent Rent Collected';
toInsert.add(actualLateRentLedger);
Property_Ledger__c expectedLateRentLedger = new Property_Ledger__c();
expectedLateRentLedger.RecordTypeId = expectedFee.Id;
expectedLateRentLedger.Name = 'Expected Property Ledger: Delinquent Rent';
expectedLateRentLedger.Property__c = pl1.Property__c;
expectedLateRentLedger.Opportunity__c = opportunity;
expectedLateRentLedger.Account__c = account;
expectedLateRentLedger.Fiscal_Year__c = fiscalYear;
expectedLateRentLedger.Fee_Amount__c = collectionAmount;
expectedLateRentLedger.Collection_Date__c = collectionDate;
expectedLateRentLedger.Description__c = 'Expected Delinquent Rent Collected';
toInsert.add(expectedLateRentLedger);
Property_Fee_Reconciler__c rentReconciler = new Property_Fee_Reconciler__c();
rentReconciler.Expected_Fee__c = expectedLateRentLedger.Id;
rentReconciler.Actual_Fee__c = actualLateRentLedger.Id;
alsoInsert.add(rentReconciler);
}
}
//If there are records in the toInsert List, insert them and the alsoInsert List into the database
if(toInsert.size() > 0) {
insert toInsert;
insert alsoInsert;
}
}
1) Yes
2) Yes. 150 DML for Transaction (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm)
3) it should work. you can refer "Test Methods and Bulk Operations" in the link below to how to test your code using 200 records
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
All Answers
1) Yes
2) Yes. 150 DML for Transaction (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm)
3) it should work. you can refer "Test Methods and Bulk Operations" in the link below to how to test your code using 200 records
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods