You need to sign in to do that
Don't have an account?
Lauren_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?
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
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);
}
}
}
}
}
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;
}
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;
}