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
Arnold Joseph TodasArnold Joseph Todas 

Contact Trigger updating Mailingaddress base on Account BillingAddress

How can i Update Mailingaddress in contact base on Account BillingAddress? and if i put null on account Name in Contact the Mailing Address should be null too

Thanks for Help!
AJ
Vijay NagarathinamVijay Nagarathinam
Hi Aj,

Try this code, It will work fine.
 
trigger getConMaildingAddress on Account (after update) 
{

list<Contact> conList = new list<Contact>();
Map<id,Account> AccountMap = new Map<id,Account>();
for(Account newAccount : Trigger.new)
{
if( (newAccount.billingstreet!= Trigger.oldmap.get(newAccount.id).billingstreet) || (newAccount.billingstate!= Trigger.oldmap.get(newAccount.id).billingstate) || (newAccount.billingcity!= Trigger.oldmap.get(newAccount.id).billingcity) || (newAccount.billingpostalcode != Trigger.oldmap.get(newAccount.id).billingpostalcode) || (newAccount.billingcountry != Trigger.oldmap.get(newAccount.id).billingcountry))
{
 AccountMap.put(newAccount.id,newAccount);
}
}
for(Contact Updatecontact : [SELECT id,Accountid,MailingAddress FROM Contact where Accountid in:AccountMap.Keyset()])
{
Updatecontact.Mailingstreet = AccountMap.get(Updatecontact.Accountid).billingstreet;
Updatecontact.Mailingcity  = AccountMap.get(Updatecontact.Accountid).billingcity;
Updatecontact.Mailingpostalcode = AccountMap.get(Updatecontact.Accountid).billingpostalcode;
Updatecontact.Mailingstate = AccountMap.get(Updatecontact.Accountid).billingstate;
Updatecontact.Mailingcountry  = AccountMap.get(Updatecontact.Accountid).billingcountry;
conList.add(Updatecontact);
}
update conList;
}

 
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,

Please find below code:
trigger demoTrigger on Contact (before update) {

set<ID> acctIds= new set<ID>();

for(Contact ct:trigger.new)
{
acctIds.add(ct.accountID);

}


List<Account> accts=[select id, name,Text1__c,BillingAddress,BillingStreet from Account where id in :acctids];

for(contact ct:trigger.new)
{
if(accts.size()>0)
{

ct.Mailingstreet =accts[0].billingstreet;
}
else
{
ct.Mailingstreet ='';
}

}
}
Kavya MaalikaKavya Maalika
@Vijay Nagarathinam

Hi can you tell me how to write test class for the code