function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepu Gupta 29Deepu Gupta 29 

Parent opportunity Checkbox needs to be true

​Hi All,

I have Req where i need to update Parent Object(Opportunity) Checkbox when the child object(custom object) checkbox is true.There is lookup relationship between both the objects.
I have achive this via below code :

trigger RRRequired on ESA_Result__c (after insert, after update) {
    list<Opportunity> listofParentObjToUpdate;
    set<Id> setOfids = new set<Id>();
    for(ESA_Result__c childobj :trigger.new){
     if(childobj.ESA_R_R_Required_EI__c==true){
            setOfids.add(childobj.ParentOpportunity_EI__c);// you child object must having parent object field, put that field Api Name here
     }
    }
   if(!setOfids.isEmpty()) {
                list<opportunity> listOfParentObj = [select id,ESA_R_R_Required_EI__c from opportunity where Id =: setOfids];
                listofParentObjToUpdate = new list<opportunity>();
                  
                    for(opportunity parent : listOfParentObj) {
                        parent.ESA_R_R_Required_EI__c = true;
      listofParentObjToUpdate.add(parent);
                    }
                }
            Update listofParentObjToUpdate;
}

Now the issue is for this custom object.Because its parent is child opp and i want to update that checkbox on its parent opportunity.
for ex.
Result : Custom object -> Child Opp -> Parent Opp
and i want to update the checkbox true whenever Result custom object checkbox is true.

Any suggestion?
 
VamsiVamsi
Hi Deepika,

Would you like to update opportunity to True when a relative child record ESA_R_R_Required_EI__c equals True or If its false then update relative parent record opportunity to False right ?
 
VamsiVamsi
What if your parent opportunity record has multiple child records with ESA_R_R_Required_EI__c  True or False then based on what criteria you would like to update the parent opportunity record ?
Deepu Gupta 29Deepu Gupta 29
Ok.yaa it will have multiple child and that checkbox needs to be true if any of its child record has ESA_R_R_Required_EI__c = true.
VamsiVamsi
So right now is your code causing an issue, If so whats the issue ? 

Also, Instead of after insert and update you can make use of before event to avoid recursive 
Deepu Gupta 29Deepu Gupta 29
Actually its updating its direct parent which is a child Opp checkbox and not updating the parent.so how can i change my code that it will update the parent opp checkbox field not the child opp.
VamsiVamsi
Can you let me know the parent opportuity field API name on Child opportunity and also the API name of Check box field on parent opportunity 
Deepu Gupta 29Deepu Gupta 29
Checkbox is this one ESA_R_R_Required_EI__c.and child we are creating on the basis of record type.API name is 'Name' on child opp of parent.
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Deepika,

Here is a Trigger which I have tested with several possiable scenarios.
Parent Object : Contact
           Field   : Checked_Checkbox_Exists__c
Child Object   : Credit__c
           Field   : Check_box__c 
Requirement   : Whenever there is a child record for parent with Check_box__c = TRUE then update Checked_Checkbox_Exists__c = TRUE on Parent Object as well.
Trigger Code:

Hope this helps!
 
HARSHIL U PARIKHHARSHIL U PARIKH
I am not sure why it doesn't allow me to send the code here..
This is the message I am getting - "The operation is not permitted. If you received this in error, please contact info@developerforce.com (code: B2)"
VamsiVamsi
Hi Govind,

She would like to update the check box on parent opportunity on child opp that relates to multiple child records. So we need the API name of parent opp on child opp 
Deepu Gupta 29Deepu Gupta 29
Hi Govind,
Not Recieved the code.can you try via attachment?
 
VamsiVamsi
Deepika,

Is there a self lookup from opp to opp ?
Deepu Gupta 29Deepu Gupta 29
No vamshi.no self lookup created.we are creating parent and child opp on the basis of the record type.I have modified my code.please have a look and suggest.its not updating the parent opp checkbox to true where parent opp = 'Application'.
trigger RRRequired on ESA_Result__c (after insert, after update) {
    list<Opportunity> listofParentObjToUpdate;
    set<Id> setOfids = new set<Id>();
    //RecordType rt = [select Id,Name,DeveloperName from RecordType where DeveloperName = 'Application' and SobjectType = 'Opportunity' limit 1];


    for(ESA_Result__c childobj :trigger.new){
     if(childobj.ESA_R_R_Required_EI__c==true){
            setOfids.add(childobj.ParentOpportunity_EI__c);
     }
    }
   if(!setOfids.isEmpty()) {
   list<opportunity> listOfParentObj = [select id,ESA_R_R_Required_EI__c,RecordType.Name from opportunity where Id =: setOfids and RecordType.Name = 'Application'];
                listofParentObjToUpdate = new list<opportunity>();
                  
                    for(opportunity parent : listOfParentObj) {
                        parent.ESA_R_R_Required_EI__c = true;
      listofParentObjToUpdate.add(parent);
                    }
                }
            Update listofParentObjToUpdate;
}

 
HARSHIL U PARIKHHARSHIL U PARIKH
 I have tried posting again but no luck.
Let me know the email address and I will try to send it.
Deepu Gupta 29Deepu Gupta 29
hi govind,
id is deepikagupta0611@gmail.com.
HARSHIL U PARIKHHARSHIL U PARIKH
Check your email Deepika!