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
Balaram AdminBalaram Admin 

using apex triggers how to create a new contact record with values of account when the contact Mailing Address is null

Hi Guys,
Requirement: insert Contact Mailing address with Account Address when a create new contact and Mailing address is null...

 
Best Answer chosen by Balaram Admin
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help u
trigger ContactTrigger on Contact( before insert )
{

	Set<ID> setAccountId = new Set<ID>();
	For(Contact cont: trigger.new)
	{
		if(cont.accountId != null)
		{
			if(	cont.MailingCity == null || cont.MailingState == null || cont.MailingCountry ==null || cont.MailingPostalCode==null)
			{
				setAccountId.add(cont.accountId);
			}
		}	
	}
	
	if(setAccountId.size() > 0 )
	{
		Map<Id,Account> mapAccount = new Map<Id,Account>([select id,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet from account where id in :setAccountId ]);

		For(Contact cont: trigger.new)
		{
			if(cont.accountId != null)
			{
				if(	cont.MailingCity == null || cont.MailingState == null || cont.MailingCountry ==null || cont.MailingPostalCode==null)
				{
					if(mapAccount.containsKey(cont.accountId))
					{
						Account acc= mapAccount.get(cont.accountId);
						cont.MailingCity = acc.BillingCity;
						cont.MailingState = acc.BillingState;
						cont.MailingCountry  = acc.BillingCountry;
						cont.MailingPostalCode = acc. BillingPostalCode;
					}
				}
			}	
		}
		
	}
	

}
Please check below post for trigger. I hope that will help u
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please let us k now if this will help u
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help u
trigger ContactTrigger on Contact( before insert )
{

	Set<ID> setAccountId = new Set<ID>();
	For(Contact cont: trigger.new)
	{
		if(cont.accountId != null)
		{
			if(	cont.MailingCity == null || cont.MailingState == null || cont.MailingCountry ==null || cont.MailingPostalCode==null)
			{
				setAccountId.add(cont.accountId);
			}
		}	
	}
	
	if(setAccountId.size() > 0 )
	{
		Map<Id,Account> mapAccount = new Map<Id,Account>([select id,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet from account where id in :setAccountId ]);

		For(Contact cont: trigger.new)
		{
			if(cont.accountId != null)
			{
				if(	cont.MailingCity == null || cont.MailingState == null || cont.MailingCountry ==null || cont.MailingPostalCode==null)
				{
					if(mapAccount.containsKey(cont.accountId))
					{
						Account acc= mapAccount.get(cont.accountId);
						cont.MailingCity = acc.BillingCity;
						cont.MailingState = acc.BillingState;
						cont.MailingCountry  = acc.BillingCountry;
						cont.MailingPostalCode = acc. BillingPostalCode;
					}
				}
			}	
		}
		
	}
	

}
Please check below post for trigger. I hope that will help u
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Please let us k now if this will help u
 
This was selected as the best answer
sharathchandra thukkanisharathchandra thukkani

//Create a trigger on contact object with before insert and before update event.
trigger ContactTrigger on Contact(before insert, before update){

//Create list of account id and query on Account obejct with details you need and then add then you can use below code
for(Conatct c : trigger.new){
 for(Account aa: queriedAccountList){
   if(c.AccountId == aa.Id){
   c.MailingCity = aa.MailingCity;
    }
}
}
}
Balaram AdminBalaram Admin
Thanks amit...