You need to sign in to do that
Don't have an account?

trigger on contact (lookup to opportunity)
Hi,
I have added opportunity as a lookup in contact. I want my description on contact to be updated with the related opportunity's stage name. My following code is not working. Please let me know where I went wrong:
trigger populateOppStage on Contact (before insert, before update) {
Set<ID> opps = New Set<ID>();
for (contact con:trigger.new){
opps.add(con.opportunity__r.Id);
}
Map<Id, String> mapOppToStage = New Map<Id, String>();
for(opportunity opp:[SELECT ID, stagename
FROM opportunity WHERE ID IN:opps]){
mapOppToStage.put(opp.id, opp.stagename);
}
for (contact c:trigger.new){
if(mapOppToStage.containsKey(c.opportunity__r.Id)){
c.Description= mapOppToStage.get(c.Opportunity__r.id);
}
}
}
I have added opportunity as a lookup in contact. I want my description on contact to be updated with the related opportunity's stage name. My following code is not working. Please let me know where I went wrong:
trigger populateOppStage on Contact (before insert, before update) {
Set<ID> opps = New Set<ID>();
for (contact con:trigger.new){
opps.add(con.opportunity__r.Id);
}
Map<Id, String> mapOppToStage = New Map<Id, String>();
for(opportunity opp:[SELECT ID, stagename
FROM opportunity WHERE ID IN:opps]){
mapOppToStage.put(opp.id, opp.stagename);
}
for (contact c:trigger.new){
if(mapOppToStage.containsKey(c.opportunity__r.Id)){
c.Description= mapOppToStage.get(c.Opportunity__r.id);
}
}
}
From trigger.new you can access only the current record references but you can't get the parent record details.
opportunity__r.Id ==> It's referring the parent details. So based on your code it will be null.
Simply you have to use the opportunity__c in code because this is will return the lookup parent id .
Please try the below code:
Thanks,
Maharajan.C
All Answers
From trigger.new you can access only the current record references but you can't get the parent record details.
opportunity__r.Id ==> It's referring the parent details. So based on your code it will be null.
Simply you have to use the opportunity__c in code because this is will return the lookup parent id .
Please try the below code:
Thanks,
Maharajan.C
Thanks for your help here. Really appreciate it to bridge my knowledge gap.