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
imishraimishra 

Updating child picklist from parent picklist

I have a master detail relationship between Opportunity(Parent) and a custom object(Child).

The child object has the same picklist field with same values as Opportunity StageName.

 

 

I want the stage value in child object to change automatically whenever the StageName in parent object changes.

 

Can anyone please tell me how to do this.

 

Thanks.

Laxman RaoLaxman Rao

Write a trigger on Opportunity which will fire after update.

In the trigger :

1 Check Opportunity StageName is changed

2 If it is changed then query all the children and update the child StageName to Opportunity StageName.

Hengky IlawanHengky Ilawan

Just curious, why not using a formula field instead?

 

Regards,

Hengky

imishraimishra

Below is the trigger i wrote, but it is not working. And here i am updating a single value, how can i do it for multiple values.

 

 

trigger updateDM on Opportunity(after update) {
    Map<Id, Opportunity> accts = new Map<Id, Opportunity>();       
    for (Integer i = 0; i < Trigger.new.size(); i++) {   
        if (Trigger.old[i].StageName != Trigger.new[i].StageName)
                   accts.put(Trigger.old[i].id,Trigger.new[i]);       
                 }
            
     List<Deal_Memo__c> updatedEstimate = new List<Deal_Memo__c>();   
     for (Deal_Memo__c c : [SELECT id, Opportunity1__c FROM Deal_Memo__c WHERE Opportunity1__c                            
                     in :accts.keySet()]) {       
           c.Stage__c = 'Closed Won';               
           updatedEstimate.add(c);   
       }              
    update updatedEstimate;  
}

Laxman RaoLaxman Rao

Try this :

 

This will work all the Opportunities.

 

trigger updateDM on Opportunity(after update) {
Map<Id, Opportunity> accts = new Map<Id, Opportunity>();
for (Integer i = 0; i < Trigger.new.size(); i++) {
if (Trigger.old[i].StageName != Trigger.new[i].StageName)
accts.put(Trigger.old[i].id,Trigger.new[i]);
}

List<Deal_Memo__c> updatedEstimate = new List<Deal_Memo__c>();
for (Deal_Memo__c c : [SELECT id, Opportunity1__c FROM Deal_Memo__c WHERE Opportunity1__c
in :accts.keySet()]) {
Opportunity opp = accts.get(c.Opportunity1__c);
c.Stage__c = opp.StageName;
updatedEstimate.add(c);
}
update updatedEstimate;
}

imishraimishra

Still not working. Can u tell me where m i going wrong

 

asish1989asish1989

Hi

  Try this 

    This is the simplest trigger 

         

trigger StagevalueUpadte on Opportunity (after update) {
    List<Child__c> childList = new List<Child__c>();
    List<Child__c> childId = new List<Child__c>();
    for(Opportunity opt:Trigger.new){
        childList = [select id, Opportunity__c, name, Stage__c from Child__c where Opportunity__c = :opt.id];
        
    for(Child__c chld :childList ){
        if(chld .Stage__c != opt.StageName){
            chld.Stage__c = opt.StageName;
            childId.add(chld);
            
        }    
    }
  }  
    if(childId != null & childId.Size()> 0)
    upsert childId;

}

 

Did this post answers  your problem , if so please mark it solved so that others get benifited .

 

Thanks

imishraimishra

Still Not working. The stage is not getting updated on child object.

Hengky IlawanHengky Ilawan

Could you provide more information rather than stated it is not working, please?

Debug Info? Error message if any?

 

Hengky

imishraimishra

No error message but my child record is not getting updated when i change the stage in Opportunity.

asish1989asish1989

Hi

  Try my code Its working ..I have tested it .....

  you make sure that the filed name in the code must be match with yours ...and after changing stage value in the Opportunity save it and refress the child record ...

 

let me know still there is some issue

 

 

Thanks

 

 

 

 

 

Hengky IlawanHengky Ilawan

Maybe add some System.Debug() call to output the variables to Debug log And then go to Developer Console to trace the trigger run, or go to Setup | Administration Setup | Monitoring | Debug Logs and see what's in there.

 

Regards,

Hengky

Laxman RaoLaxman Rao

Try this and let me know:

 

trigger updateDM on Opportunity(after update) {
set<Id> oppIds = new set<Id>();
for (Opportunity newOpp : Trigger.newMap.Values())
{
Opportunity oldOpp = Trigger.newMap.get(newOpp.Id);
if (newOpp.StageName != oldOpp.StageName) {
oppIds.add(Trigger.new[i].Id);
}
}

List<Deal_Memo__c> updatedEstimate = new List<Deal_Memo__c>();
for (Deal_Memo__c c : [SELECT id, Opportunity1__c FROM Deal_Memo__c WHERE Opportunity1__c in :oppIds]) {
Opportunity opp = Trigger.newMap.get(c.Opportunity1__c);
c.Stage__c = opp.StageName;
updatedEstimate.add(c);
}
update updatedEstimate;
}

imishraimishra

The triggers are working fine.

 

But in my case, i have written a trigger which creates a record inside deal memo when opportunity stage changes to Negotiation/Review.

And when i close my opportunity it should reflect in deal also, which is not happeneing now with this trigger.