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
SambitNayakSambitNayak 

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);
        }
    }
}
Best Answer chosen by SambitNayak
Maharajan CMaharajan C
Hi Nayak,

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:
 
trigger populateOppStage on Contact (before insert, before update) {
    Set<ID> opps = New Set<ID>();
    for (contact con:trigger.new){
        if(con.opportunity__c != null)
        	opps.add(con.opportunity__c);
    }
    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__c)){
            c.Description= mapOppToStage.get(c.opportunity__c);
        }
    }  
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Nayak,

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:
 
trigger populateOppStage on Contact (before insert, before update) {
    Set<ID> opps = New Set<ID>();
    for (contact con:trigger.new){
        if(con.opportunity__c != null)
        	opps.add(con.opportunity__c);
    }
    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__c)){
            c.Description= mapOppToStage.get(c.opportunity__c);
        }
    }  
}

Thanks,
Maharajan.C
This was selected as the best answer
SambitNayakSambitNayak
Hi Maharajan,
Thanks for your help here. Really appreciate it to bridge my knowledge gap.