0

I have a small problem because I need to write a trigger which will update checkbox when the related object will be deleted.

Between those object is lookup relation. So only what I need is when Object A does not have any related Objects B then mark the checkbox which is exist on Object A to False. Obviously Object A might have a few related Objects B. So the checkbox should be change after if number of related Objects B will be 0.

Anyone could help ?

2 Answers 2

0

You need to write trigger Object B after delete event. Follow below code:

trigger ObjectBTrigger on ObjectB__c (after delete) {

    List<Id> parentIdList = new List<Id>();
    for(ObjectB__c ab : Trigger.old){
        parentIdList.add(ab.ObjectALookup);
    }
    List<ObjectA__c> objectAList = new List<ObjectA__c>();

    for(ObjectA__c ab : [SELECT Id, Checkbox__c (SELECT Id FROM ObjectB__r) FROM ObjectA__c WHERE Id IN :parentIdList]){
        if(ab.ObjectB__r.size() == 0){
            ab.Checkbox__c = false;
            objectAList.add(ab);
        }
    }

    update objectAList;
}
0

You need trigger on Object B in this case.

trigger triggerName on ObjectB__c (before delete) {/*...*/}

In this trigger, store all related objects A ids (parentsIds), then query objects A with related objects B:

[SELECT Id, (SELECT Id FROM ObjectB__r) FROM ObjectA__c WHERE Id IN :parentsIds]

Then update checkboxes for those Objects A that have ObjectB__r.size() == 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .