Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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){
            mapIdVsBigMachines.put(Trigger.new[i].BigMachines__Opportunity__c,Trigger.new[i]);
        }
    }

    if(!mapIdVsBigMachines.isEmpty()){
        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;
                listofParentObjToUpdate.add(parent);
        }
        Update listofParentObjToUpdate;
    }          
}
share|improve this question
    
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 at 21:18
    
It's lookup so i got to go for trigger –  Admin_BA Jun 16 at 21:44

2 Answers 2

up vote 3 down vote accepted
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){
listIds.add(childObj.BigMachines__Opportunity__c);
}

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();

}
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.