0

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;
            } 
    }
}
2

The big things to avoid are:

  • Queries inside of loops
  • DML inside of loops

That right there is 95% of bulkification, and it doesn't look like that's an issue in your code.

About the only thing I could suggest here is to use a parent-child subquery to do the heavy lifting.

ex...

if( Trigger.isUpdate || Trigger.isInsert){
    List<Lease_Payment_Term__c> lptsToExpire = new List<Lease_Payment_Term__c>();

    for(Lease__c lease :[SELECT Id, (SELECT Id FROM Lease_Payment_Terms__r WHERE Status__c = 'Active') FROM Lease__c WHERE Status__c = 'Expired' AND Id IN :trigger.new]){
        for(Lease_Payment_Term__c lpt :lease.Lease_Payment_Terms__r){
            lpt.Status__c = 'Expired';
            lptsToExpire.add(lpt);
        }
    }

    update lptsToExpire;
}

I'm taking a guess at the child relationship name, and using __r in the places I did is intentional. If you don't understand what the query is doing, or what that __r business is, then going through the documentation on Relationship Queries should help.

2
  • Thank you for the reply Derek. In the example you supplied, is it grabbing all of the Leases at once or is it going through each lease and iterating through all of the child records? thx
    – Phuc
    Nov 2 '20 at 2:33
  • @Phuc Not quite sure I understand your question. It's grabbing the Leases involved in your trigger (trigger.new) as well as all of the active Lease Payment Terms for each returned Lease (the subquery can have filters, and we're filtering on Status). The subquery causes a list of child records to be embedded in each parent record.
    – Derek F
    Nov 2 '20 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.