+ Start a Discussion
SalesforceAddictSalesforceAddict 

ow can i create trigger on contact object where if change mailing address field or enter new mailing address in new contact then it automatically add same mailing address on (Ohter Address Field)

Abhishek BansalAbhishek Bansal
Hi, 

You simply need to create a before insert and before update trigger on Contact object that will copy the value from mailing address to other address fields as per your requirement. Sample code is given below:
trigger updateAddress on Contact(before insert, before update) {
	for(Contact con : trigger.new) {
		con.other_Address_Field__c = con.Mailing_Address_Field__c;
	}
}
//Please replace the other_Address_Field__c with the API name of the address field in whch you need to copy the mailing address and also replace the Mailing_Address_Field__c with API name of Mailing Address field
Please let me know if you need any further help or information on this.

Thanks,
Abhishek Bansal.
Akshay_DhimanAkshay_Dhiman
Hi
Try this code.
 
Trigger contactAddressUpdate on Contact(before insert, before update) {
 if(Trigger.isInsert){
  for(Contact cont : trigger.new) {
   cont.other_Address_Field__c = cont.Mailing_Address_Field__c;
  }
 }
 if(Trigger.isUpdate){
  for(Contact cont : trigger.new) {
   if(cont.Mailing_Address_Field__c!=Trigger.oldMap.get(cont.Id).Mailing_Address_Field__c){
    cont.other_Address_Field__c = cont.Mailing_Address_Field__c;
   }
  }
 } 
}


if you found this answer helpful then please mark it as best answer so it can help others.   

Thanks 
Akshay
Ajay K DubediAjay K Dubedi
Hi,

Try this hoe it will help you.

trigger UpdateAddress on Contact (after insert, after update) 
{
 for (Contact c : Trigger.new) 
  {
 if(c.MailingAddress!=null)
      {
          c.OtherAddress   = c.MailingAddress;
          c.MailingCity    = c.MailingAddress;
          c.MailingState   = c.MailingAddress;
          c.MailingPostalCode = c.MailingAddress;
          c.MailingCountry    = c.MailingAddress;
          update c;
      }
   }
}

Thanks,
Ajay Dubedi