trigger AccountTrigger on Account (after update)
{
// Set will avoid duplicates
// here adding the account id in set
Set<Id> set_accountId = new Set<Id>();
System.debug('Set the Account Values are : ' + set_accountId); // here getting values as null in list,set and map
//List is for bulk update
List<Contact> contactList = new List<Contact>();
System.debug('List the Account Values are : ' + contactList);
// Map is having the list of account id and the list of contacts
Map<Id,List<Contact>> mp_ListContact = new Map<Id,List<Contact>>();
System.debug('Map the Account Values are : ' + mp_ListContact);
// Inside for loop we are adding the account id in the Set. Set will avoid duplicates
for(Account acc : Trigger.new)
{
if(acc.billingcity!=Trigger.oldMap.get(acc.id).billingcity)
set_accountId.add(acc.id);
System.debug('Set the Account Values are : ' + set_accountId);
}
// In this for loop we are querying 'Contact' which is having the account.
// Here we need to check the governor limits . Important about query : accountId IN : set_accountId
for(Contact con : [Select Id,AccountId,mailingcity from Contact where accountId IN : set_accountId limit 50000])
{
// Adding the key and values
// Key is accountId
// Value is conlist
// conlist is having the accountid
//
if(mp_ListContact!=null && mp_ListContact.containsKey(con.AccountId))
{
List<Contact> conList = mp_ListContact.get(con.AccountId);
conList.add(con);
mp_ListContact.put(con.AccountId, conList);
}
else
mp_ListContact.put(con.AccountId, new List<Contact>{con});
}
for(Account acc : Trigger.new)
{
if(mp_ListContact!=null && mp_ListContact.containsKey(acc.id))
{
for(Contact con : mp_ListContact.get(acc.id))
{
// Write logic to update contact as sample below :
con.mailingcity= acc.billingcity;
con.FirstName = acc.Name;
contactList.add(con);
}
}
}
if(contactList!=null && contactList.size()>0){
update contactList;
}
}
{
// Set will avoid duplicates
// here adding the account id in set
Set<Id> set_accountId = new Set<Id>();
System.debug('Set the Account Values are : ' + set_accountId); // here getting values as null in list,set and map
//List is for bulk update
List<Contact> contactList = new List<Contact>();
System.debug('List the Account Values are : ' + contactList);
// Map is having the list of account id and the list of contacts
Map<Id,List<Contact>> mp_ListContact = new Map<Id,List<Contact>>();
System.debug('Map the Account Values are : ' + mp_ListContact);
// Inside for loop we are adding the account id in the Set. Set will avoid duplicates
for(Account acc : Trigger.new)
{
if(acc.billingcity!=Trigger.oldMap.get(acc.id).billingcity)
set_accountId.add(acc.id);
System.debug('Set the Account Values are : ' + set_accountId);
}
// In this for loop we are querying 'Contact' which is having the account.
// Here we need to check the governor limits . Important about query : accountId IN : set_accountId
for(Contact con : [Select Id,AccountId,mailingcity from Contact where accountId IN : set_accountId limit 50000])
{
// Adding the key and values
// Key is accountId
// Value is conlist
// conlist is having the accountid
//
if(mp_ListContact!=null && mp_ListContact.containsKey(con.AccountId))
{
List<Contact> conList = mp_ListContact.get(con.AccountId);
conList.add(con);
mp_ListContact.put(con.AccountId, conList);
}
else
mp_ListContact.put(con.AccountId, new List<Contact>{con});
}
for(Account acc : Trigger.new)
{
if(mp_ListContact!=null && mp_ListContact.containsKey(acc.id))
{
for(Contact con : mp_ListContact.get(acc.id))
{
// Write logic to update contact as sample below :
con.mailingcity= acc.billingcity;
con.FirstName = acc.Name;
contactList.add(con);
}
}
}
if(contactList!=null && contactList.size()>0){
update contactList;
}
}
No comments:
Post a Comment