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;

    }

 }
}

```

}