+ Start a Discussion
s_macs_mac 

trigger on child object to update record field based on the field from all the related child records

I have a trigger on event to update opportunity checkbox field , based on the values from all the related event records  check box field
suppose if opp has three events->if any one of the related event record has check box==true then update opp.checkbox==true else false
Best Answer chosen by s_mac
Bhanu MaheshBhanu Mahesh
Hi S_mac,

Lets suppose API Name of  checkBox field on event be ChckBox__c and on opportunity be oppChckBox__c 


trigger UpdateCheckBoxOnOpp on Event (after insert,after update) {
    Set<Id> oppIds = new Set<Id>();
    List<Opportunity> lstOppToUpdate = new List<Opportunity>();
    for (Event e : Trigger.new) {
            if (e.WhatId != null && string.valueof(e.WhatId).startsWith('006')) {
                oppIds.add(e.WhatId);
            }
        }
        if(!oppIds.isEmpty()){
            for(Opportunity opp:[SELECT Id,oppChckBox__c,(SELECT Id FROM Events WHERE ChckBox__c = true) FROM Opportunity WHERE Id IN : oppIds]){
                if(opp.Events.size() > 0){
                    if(!opp.oppChckBox__c){
                        opp.oppChckBox__c = true;
                        lstOppToUpdate.add(opp);
                    }
                }
                else if(opp.oppChckBox__c){
                    opp.oppChckBox__c = false;
                    lstOppToUpdate.add(opp);
                }
            }
        }
        
    if(lstOppToUpdate.size() > 0){
        update lstOppToUpdate;
    }
}

Regards,m
Bhanu Mahesh

All Answers

ra811.3921220580267847E12ra811.3921220580267847E12
Hi
trigger eventDemo on event (after insert, after update)
{
    Set<Id> oppIds = new Set<Id>();
    Map<Id,Opportunity> mapOpptnty = new Map<Id,Opportunity>();
for(Event eve:trigger.new)
{
if (t.WhatId != null && string.valueof(t.WhatId).startsWith('006') ) {
                oppIds.add(t.WhatId);
            }
}

if(!oppIds.isEmpty()){
            for(Opportunity opp:[SELECT Id,test__c FROM Opportunity WHERE Id IN : oppIds]){
                mapOpptnty.put(opp.Id,opp);
            }
        }
        
        for (Event t : Trigger.new) {
            if (t.WhatId != null && string.valueof(t.WhatId).startsWith('006') && mapOpptnty != null && mapOpptnty.get(t.WhatId) != null) {
                Opportunity opp = mapOpptnty.get(t.WhatId);
                opp.test__c = t.test__c;
                mapOpptnty.put(opp.Id,opp);
            }
        }
        
        if(mapOpptnty.values().size() > 0){
        update mapOpptnty.values();
    }
}
s_macs_mac
Hii  thanks for the reply
but here we are considering only the event records(for (Event t : Trigger.new) which we are inserting or updating but not all the related events of opportunity.
i want the opp.checkbox field==true if any one of all the  related events(including already existing related events or currently inserting or editing event records)  have checkbox== true.
Bhanu MaheshBhanu Mahesh
Hi S_mac,

Lets suppose API Name of  checkBox field on event be ChckBox__c and on opportunity be oppChckBox__c 


trigger UpdateCheckBoxOnOpp on Event (after insert,after update) {
    Set<Id> oppIds = new Set<Id>();
    List<Opportunity> lstOppToUpdate = new List<Opportunity>();
    for (Event e : Trigger.new) {
            if (e.WhatId != null && string.valueof(e.WhatId).startsWith('006')) {
                oppIds.add(e.WhatId);
            }
        }
        if(!oppIds.isEmpty()){
            for(Opportunity opp:[SELECT Id,oppChckBox__c,(SELECT Id FROM Events WHERE ChckBox__c = true) FROM Opportunity WHERE Id IN : oppIds]){
                if(opp.Events.size() > 0){
                    if(!opp.oppChckBox__c){
                        opp.oppChckBox__c = true;
                        lstOppToUpdate.add(opp);
                    }
                }
                else if(opp.oppChckBox__c){
                    opp.oppChckBox__c = false;
                    lstOppToUpdate.add(opp);
                }
            }
        }
        
    if(lstOppToUpdate.size() > 0){
        update lstOppToUpdate;
    }
}

Regards,m
Bhanu Mahesh
This was selected as the best answer
s_macs_mac
Hi Bhanu,

 Thanks for the reply :) it worked