You need to sign in to do that
Don't have an account?
imishra
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.
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.
Just curious, why not using a formula field instead?
Regards,
Hengky
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;
}
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;
}
Still not working. Can u tell me where m i going wrong
Hi
Try this
This is the simplest trigger
Did this post answers your problem , if so please mark it solved so that others get benifited .
Thanks
Still Not working. The stage is not getting updated on child object.
Could you provide more information rather than stated it is not working, please?
Debug Info? Error message if any?
Hengky
No error message but my child record is not getting updated when i change the stage in Opportunity.
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
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
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;
}
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.