You need to sign in to do that
Don't have an account?
s_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
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
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
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();
}
}
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.
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
Thanks for the reply :) it worked