I have 2 objects Opportunity(Parent) and Bigmachines (child), i have check box field (flagPhyscianProctor__c) on both the objects so when this check box is checked on any of it's child records then the parent field (flagPhyscianProctor__c) has to be checked and viceversa, here is my trigger, it works in some scenarios but in one of my scenario where i have Opp and it has 4 quotes where i enabled checkbox on 2 of the quotes,when i edit and save the opp the check box got updated to true but when i unchecked it on one of the quote where on the other quote it is still set to true the check box on opp gets deactivated, somehow it's not validating all the child records, not sure what was the issue

trigger PhyscianProctor on BigMachines__Quote__c (after insert, after update) {
    list<Opportunity> listofParentObjToUpdate;
    set<Id> setOfids = new set<Id>();
    Map<Id,BigMachines__Quote__c> mapIdVsBigMachines = new  Map<Id,BigMachines__Quote__c();
    for(Integer i=0;i<Trigger.new.size();i++){
        if(Trigger.new[i].flagPhyscianProctor__c==true || Trigger.new[i].flagPhyscianProctor__c==false){

        list<Opportunity> listOfParentObj = [select id,flagPhyscianProctor__c from Opportunity where Id IN: mapIdVsBigMachines.keySet()];

        listofParentObjToUpdate = new list<Opportunity>();
        for(Opportunity parent : listOfParentObj) {
            parent.flagPhyscianProctor__c = mapIdVsBigMachines.get(parent.id).flagPhyscianProctor__c;
        Update listofParentObjToUpdate;
  • Is the BigMachines__Quote a detail in master-detail relationship? If that's the case you could use a [help.salesforce.com/… workflow) and save yourself 1 trigger. Or maybe a rollup summary field with "count of flags on child records"?
    – eyescream
    Jun 16, 2014 at 21:18
  • It's lookup so i got to go for trigger
    – Admin_BA
    Jun 16, 2014 at 21:44

2 Answers 2

trigger PhyscianProctor on BigMachines__Quote__c (after insert, after update) {
Map<ID, Opportunity> oppsToUpdate = new Map<ID, Opportunity>();   
List<Id> listIds = new List<Id>();
Boolean isExists = false;

list<BigMachines__Quote__c> bqList = new list<BigMachines__Quote__c>();

for (BigMachines__Quote__c childObj : Trigger.new){

oppsToUpdate = new Map<Id, Opportunity>([SELECT id, flagPhyscianProctor__c,(SELECT ID, flagPhyscianProctor__c FROM BigMachines__BigMachines_Quotes__r) FROM Opportunity WHERE ID IN :listIds]);

for (Opportunity opp: oppsToUpdate.values()){
isExists = false;
for(BigMachines__Quote__c  bq :opp.BigMachines__BigMachines_Quotes__r){
 if(bq.flagPhyscianProctor__c == true){
  opp.flagPhyscianProctor__c = true;
  isExists = true;
 if(isExists == false){
  opp.flagPhyscianProctor__c = false;

update oppsToUpdate.values();


Your trigger will only be passed the child object(s) that have been inserted or updated but your requirement is that that you check if any child object has the flag checked. So you will need to include a query of BigMachines__Quote__c to find that out.

You also need a second trigger on Opportunity for the "viceversa" case.

You must log in to answer this question.

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