0

Obj11 is parent of Obj1.

If Obj11.Lock = "Lock ALL" -> Update all child records Obj1, with Obj1.Lock= true;

If Obj11.Lock = "Unlock ALL" -> Update all child records Obj1, with Obj1.Lock= false;

I have this following trigger on Obj11 to do so:

trigger TPI_Lock_All_MCCP on MC_Cycle_vod__c(after insert, after update) {
    Set < Id > Lock_Ids = new Set < Id > ();
    Set < Id > Unlock_Ids = new Set < Id > ();
    for (MC_Cycle_vod__c mc: trigger.new) {
        if (mc.TPI_Lock_All__c == 'Lock All MCCP')
            Lock_Ids.add(mc.Id);
        else if (mc.TPI_Lock_All__c == 'Unlock All MCCP')
            Unlock_Ids.add(mc.Id);
    }
    List < MC_Cycle_Plan_vod__c > MCCPupd = new List < MC_Cycle_Plan_vod__c > ();
    for (MC_Cycle_Plan_vod__c mccp: [Select Id, Lock_vod__c, Goal_Edit_Status_vod__c from MC_Cycle_Plan_vod__c where Cycle_vod__c in: Lock_Ids]) {
        mccp.Lock_vod__c = true;
        MCCPupd.add(mccp);
    }
    for (MC_Cycle_Plan_vod__c mccp: [Select Id, Lock_vod__c, Goal_Edit_Status_vod__c from MC_Cycle_Plan_vod__c where Cycle_vod__c in: Unlock_Ids]) {
        mccp.Lock_vod__c = false;
        mccp.Goal_Edit_Status_vod__c = 'New_vod';
        MCCPupd.add(mccp);
    }
    if (MCCPupd.size() > 0)
        update MCCPupd;
}

but here I've used the same List MCCPupd to populate child records Obj1 for both the conditions Lock All and Unlock All. Is there any risk in this approach, or I should keep two different List<Obj1> and update the same after checking list.size()>0 ?

Please advise.

1

What you've done is fine.

for(MC_Cycle_Plan_vod__c mccp : [Select Id,Lock_vod__c,Goal_Edit_Status_vod__c from 
 MC_Cycle_Plan_vod__c where Cycle_vod__c in : Lock_Ids]){

The above query creates one list of records which you add to MCCPupd. The below query creates an entirely different list which you also add to it:

for(MC_Cycle_Plan_vod__c mccp : [Select Id,Lock_vod__c,Goal_Edit_Status_vod__c from 
 MC_Cycle_Plan_vod__c where Cycle_vod__c in : Unlock_Ids]){

It's always good practice to combine your DML when possible. So putting it all into:

if(MCCPupd.size()>0) update MCCPupd;

isn't a problem. I'll add that if you're going to make a check, it's preferable to use isEmpty() over size(). Neither should be necessary. If there are no records to update, you won't get a runtime error.

| improve this answer | |

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.