I am trying to update the children records when the parent field is updated. The code I have works but I was wondering is it the optimized for data loads. So if a user uploads 1000 Lease records and each Lease record has 20 child records. Will the code I have work well? Thanks.
declared at class level: List <Lease_Payment_Term__c > lpt = new ListLease_Payment_Term__c >();
global void bulkAfter(){
if( Trigger.isUpdate || Trigger.isInsert){
List <Lease_Payment_Term__c > lpt = new ListLease_Payment_Term__c >();
List<Id> leaseIds = new List<Id>();
for( SObject record : Trigger.new ){
Lease__c newlease = ( Lease__c ) record;
if( newlease.Status__c == 'Expired'){
leaseIds.add( newlease.Id);
}
}
lpt = [Select id,Status__c from Lease_Payment_Term__c
WHERE Status__c = 'Active' and Lease__c IN : leaseIds
];
if( lpt.size() > 0){
for(Lease_Payment_Term__c lpts : lpt){
lpts.status__c = 'Expired';
}
update lpt;
}
}
}