1

There is a contact lookup field in opportunity object. When ever user selects the contact value, the shipping custom fields in the opportunity should be auto populated with the mailing address values of contact object record. I have written the following code. It is working.

I am wondering on how to include trigger.oldmap and trigger.newmap in this code to make sure that trigger doesn't work for old values. I mean if the values are not changed.

trigger populateOpportunityfromContact on Opportunity (before insert , before update)

{

if(trigger.isbefore && trigger.isinsert)
{
  Set<ID> ConIds = new Set<ID>();

for(Opportunity opp : trigger.new)
{
      ConIds.add(opp.RSM_Shipping_Contact__c);    
}

list <contact> conlist = [SELECT Email,Id,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,Phone FROM Contact where id IN:ConIds];

 MAP<ID ,contact> mapCon = new MAP<ID ,Contact>();
 for(Contact c : conlist)
 {
    mapcon.put(c.id,c);
 }

 for(Opportunity opp : trigger.new)
 {
    if(opp.RSM_Shipping_Contact__c!=null)
    {
      if(mapcon.containskey(opp.RSM_Shipping_Contact__c))
      {
        contact c = mapcon.get(opp.RSM_Shipping_Contact__c);
        opp.Shipping_Street__c = c.MailingStreet;
        opp.Shipping_City__c = c.MailingCity;
        opp.Shipping_State__c = c.MailingState;
        opp.Shipping_Country__c= c.MailingCountry;
        opp.Shipping_postal_code__c = c.MailingPostalCode;
        opp.Shipping_Email__c = c.Email;
        opp.Shipping_Phone__c = c.phone;
      }
    }

    else
    {

      opp.Shipping_Street__c = null;
      opp.Shipping_City__c = null;
      opp.Shipping_State__c = null;
      opp.Shipping_Country__c= null;
      opp.Shipping_postal_code__c = null;
      opp.Shipping_Email__c = null;
      opp.Shipping_Phone__c = null;

    }

 }
}

}

2

You can use an if condition if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c)

This will make sure that code is executed only if contact lookup is changed.

 if(trigger.isbefore && trigger.isinsert)
{
  Set<ID> ConIds = new Set<ID>();

for(Opportunity opp : trigger.new)
{
      ConIds.add(opp.RSM_Shipping_Contact__c);    
}

list <contact> conlist = [SELECT Email,Id,MailingCity,MailingCountry,MailingPostalCode,MailingState,MailingStreet,Phone FROM Contact where id IN:ConIds];

 MAP<ID ,contact> mapCon = new MAP<ID ,Contact>();
 for(Contact c : conlist)
 {
    mapcon.put(c.id,c);
 }

 for(Opportunity opp : trigger.new)
 {
  if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c){
    if(opp.RSM_Shipping_Contact__c!=null)
    {
      if(mapcon.containskey(opp.RSM_Shipping_Contact__c))
      {
        contact c = mapcon.get(opp.RSM_Shipping_Contact__c);
        opp.Shipping_Street__c = c.MailingStreet;
        opp.Shipping_City__c = c.MailingCity;
        opp.Shipping_State__c = c.MailingState;
        opp.Shipping_Country__c= c.MailingCountry;
        opp.Shipping_postal_code__c = c.MailingPostalCode;
        opp.Shipping_Email__c = c.Email;
        opp.Shipping_Phone__c = c.phone;
      }
    }

    else
    {

      opp.Shipping_Street__c = null;
      opp.Shipping_City__c = null;
      opp.Shipping_State__c = null;
      opp.Shipping_Country__c= null;
      opp.Shipping_postal_code__c = null;
      opp.Shipping_Email__c = null;
      opp.Shipping_Phone__c = null;

    }
 }
 }
}
0

Sfdc biggy,

Don't you think following will be troublesome in insert trigger as it usese trigger.old.Also is that at the best location?

if(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c)

I think it should be in for(Opportunity opp : trigger.new) loop as following : for(Opportunity opp : trigger.new){ if (trigger.isinsert) ConIds.add(opp.RSM_Shipping_Contact__c);
elseif(trigger.oldmap.get(opp.Id).RSM_Shipping_Contact__c != opp.RSM_Shipping_Contact__c) ConIds.add(opp.RSM_Shipping_Contact__c);

Please correct if you feel this is not appropriate. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.