Below is the sample code for the same try to change according to your requirements.
trigger CaseAfterInsertUpdate on Account(after insert, after update) {
List<Contact> accList = new List<Contact>();
Map<Id,List<Contact>> AccountMap = new Map<Id,List<Contact>>();
if(Trigger.isUpdate){
for(Contact con : [SELECT Id,AccountId FROM C0ntact WHERE AccountId IN :Trigger.newMap.keySet()])
if(!AccountMap.containskey(con.AccountId){
AccountMap.put(con.AccountId,new List<Contact>());
}
AccountMap.get(con.AccountId).add(con);
}
}
for(Account c:Trigger.new) {
system.debug('hfeffht'+c.ID);
if(!AccountMap.containskey(c.Id)) { //code for insert only
Contact con = new Contact(LastName=c.Name,
MailingStreet=c.BillingStreet,
MailingCity=c.BillingCity,
MailingPostalCode=c.BillingPostalCode,
MailingCountry=c.BillingCountry,
AccountId=c.ID);
accList.add(con);
system.debug('AccountId'+con.AccountId);
}else { //code for update account
for(Contact con : AccountMap.get(c.Id)){ //get all contacts under account and update.
con.MailingCity = c.BillingCity;
con.MailingPostalCode = c.BillingPostalCode;
con.MailingCountry = c.BillingCountry;
accList.add(con);
}
}
}
try {
upsert accList;
} catch (Exception ex) {
System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
}
}
trigger updateAccount on Contact(after update) {
Set<Id> accIds = new Set<Id>();
for(Contact con : trigger.new) {
if(con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress) {
accIds.add(con.AccountId);
}
}
if(accIds.size() > 0) {
Map<Id, Account> mapOfAccounts = new Map<Id, Account>([Select id, BillingAddress from Account where Id IN :accIds]);
List<Account> updateAccountList = new List<Account>();
for(Contact con : trigger.new) {
if(con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress && mapOfAccounts.containsKey(con.AccountId)) {
mapOfAccounts.get(con.AccountId).BillingAddress = con.MailingAddress;
updateAccountList.add(mapOfAccounts.get(con.AccountId));
}
}
if(updateAccountList.size() > 0){
update updateAccountList;
}
}
}
//Please use the appropriate fields in place of MailingAddress(Contact) and BillingAddress(Acccount) as per your requirement and data strucgure and take care of the syntax.
trigger updateAccount on Contact(after insert, after update) {
Set<Id> accIds = new Set<Id>();
for(Contact con : trigger.new) {
if(trigger.isInsert && con.AccountId != null) {
accIds.add(con.AccountId);
}
else if(trigger.isUpdate && con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress) {
accIds.add(con.AccountId);
}
}
if(accIds.size() > 0) {
Map<Id, Account> mapOfAccounts = new Map<Id, Account>([Select id, BillingAddress from Account where Id IN :accIds]);
List<Account> updateAccountList = new List<Account>();
for(Contact con : trigger.new) {
if(con.AccountId != null && trigger.oldMap.get(con.id).MailingAddress != con.MailingAddress && mapOfAccounts.containsKey(con.AccountId)) {
mapOfAccounts.get(con.AccountId).BillingAddress = con.MailingAddress;
updateAccountList.add(mapOfAccounts.get(con.AccountId));
}
}
if(updateAccountList.size() > 0){
update updateAccountList;
}
}
}
This trigger will update the BillingAddress on Account if MailingAddress is changed on Contact and this is what you mentioned in your requirement. I am not sure what do you mean by your last reply. If your requirement is something else then you can modify the code as per your requirement. Please take this code as a guidance and not as a complete solution. If that solves your query then please close this question so that it can help others.
Ajay sir can you please tell how i can find recent contact when contact comes in bulk and then recent contact address change to account adress means i want this code for bulk only recent contact address change the account adress when contact insert in bulk
Hope this will help you
trigger UpdateContactAdd on Contact(after Update) {
if(trigger.isAfter)
{
ChangeAccAddress.usingContactAddress(trigger.new);
}
}
public class ChangeAccAddress {
public static void usingContactAddress(List<Contact> conList)
{
map<Id,Contact> conidVsConMap = new map<Id,Contact>
(
[Select Id,AccountId, MailingStreet,MailingCity, MailingState, MailingCountry, MailingPostalCode from Contact
WHERE Id IN:conList]
);
List<Account> accList = new List<Account>();
for(Contact con : conList)
{
Account accObj = new Account();
accObj.Id = conidVsConMap.get(con.Id).AccountId;
accObj.BillingStreet = conidVsConMap.get(con.Id).MailingStreet;
accObj.BillingState = conidVsConMap.get(con.Id).MailingState;
accObj.BillingPostalCode = conidVsConMap.get(con.Id).MailingPostalCode;
accObj.BillingCountry = conidVsConMap.get(con.Id).MailingCountry;
accObj.BillingCity = conidVsConMap.get(con.Id).MailingCity;
accList.add(accObj);
}
system.debug('account Address>>>>>>>>>' + accList);
if(accList.size() > 0)
update accList ;
}
}
Thank you
Ajay Dubedi
All Answers
Below is the sample code for the same try to change according to your requirements.
Best Regards,
Sandhya
Please try with the below code:
Let me know in case there is an issue.
Thanks,
Abhishek Basal.
Hope this will help you
trigger UpdateContactAdd on Contact(after Update) {
if(trigger.isAfter)
{
ChangeAccAddress.usingContactAddress(trigger.new);
}
}
public class ChangeAccAddress {
public static void usingContactAddress(List<Contact> conList)
{
map<Id,Contact> conidVsConMap = new map<Id,Contact>
(
[Select Id,AccountId, MailingStreet,MailingCity, MailingState, MailingCountry, MailingPostalCode from Contact
WHERE Id IN:conList]
);
List<Account> accList = new List<Account>();
for(Contact con : conList)
{
Account accObj = new Account();
accObj.Id = conidVsConMap.get(con.Id).AccountId;
accObj.BillingStreet = conidVsConMap.get(con.Id).MailingStreet;
accObj.BillingState = conidVsConMap.get(con.Id).MailingState;
accObj.BillingPostalCode = conidVsConMap.get(con.Id).MailingPostalCode;
accObj.BillingCountry = conidVsConMap.get(con.Id).MailingCountry;
accObj.BillingCity = conidVsConMap.get(con.Id).MailingCity;
accList.add(accObj);
}
system.debug('account Address>>>>>>>>>' + accList);
if(accList.size() > 0)
update accList ;
}
}
Thank you
Ajay Dubedi
Just added the code for insert This trigger will update the BillingAddress on Account if MailingAddress is changed on Contact and this is what you mentioned in your requirement. I am not sure what do you mean by your last reply. If your requirement is something else then you can modify the code as per your requirement. Please take this code as a guidance and not as a complete solution. If that solves your query then please close this question so that it can help others.
Thanks,
Abhishek Bansal.