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
Lauren_HLauren_H 

Trigger on child object to update parent object.

I have a situation where I believe I need to run a trigger on a child object to update a parent object field. I have a child object Proposal_Standard_Option that has a checkbox field Is_Optional__c. If the is optional field is checked, I need the same field (same field type and field name) to also be checked on the parent object, Proposal_Line_Item. Does anyone have an idea how this can be done?
Sagarika RoutSagarika Rout
1. Create one trigger  on child object with criteria after insert , after update
2. Through parent Id query all the fields of parent which you want to update(like Is_Optional__c)
3. Then update parent by making the checkbox as true.(Is_Optional__c = 'true')
4. It will work when you will create any child record or else update any child record.


Regards
Sagarika Rout
SFDC Developer
Lauren_HLauren_H
Thanks so much for the input. I tried this trigger, but I'm getting errors. Any thoughts?

trigger Updateisoptionalonproposallineitem on Proposal_Standard_Option__c (after insert, after update) {
    set<Id> setOfids = new set<Id>();
    for(Proposal_Line_Item op=trigger.new;
    ;
        )if(Proposal_Standard_Option.Is_Optional__c==true){
            setOfids.add(op.Id);
        }
    }
if(!setOfids.isEmpty()) {
                list<Proposal_Standard_Option__c> listOfProposalStandardOption = [select Proposal_Line_Item__c,id,Is_Optional__c from Proposal_Standard_Option__c where Proposal_Line_Item__c =: setOfids];
                    map<Id, Boolean> map_ProposalLineItemId_HasChildActive = new map<Id, Boolean>();
                   
                    for(Proposal_Standard_Option__c Option : listOfOption) {
                        if(Proposal_Standard_Option.Is_Optional__c == true) {
                            map_ProposalLineItemId_HasChildActive.put(ProposalStandardOption.Proposal_Line_Item__c , true);
                        }
                    }
                }
            }
}
Sagarika RoutSagarika Rout
I have modifield your code. Please have a look and let me know if you still get error.


trigger Updateisoptionalonproposallineitem on Proposal_Standard_Option__c (after insert, after update) {
    set<Id> setOfids = new set<Id>();
    for(Proposal_Standard_Option__c childobj :trigger.new){
     if(childobj.Is_Optional__c==true){
            setOfids.add(childobj.parentFieldApiName);// you child object must having parent object field, put that field Api Name here
        }
    }
   if(!setOfids.isEmpty()) {
                list<Proposal_Line_Item__c> listOfParentObj = [select id,Is_Optional__c from Proposal_Line_Item__c where Id =: setOfids];
    list<Proposal_Line_Item__c> listofParentObjToUpdate = new list<Proposal_Line_Item__c>();
                   
                    for(Proposal_Line_Item__c parent : listOfParentObj) {
                        parent.Is_Optional__c = true;
      listofParentObjToUpdate.add(parent);
                    }
                }
            Update listofParentObjToUpdate;
}
Lauren_HLauren_H
Thanks. The error I'm getting now is that variable listofParentObjToUpdate does not exist.
Sagarika RoutSagarika Rout
Now you try....................


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