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
B1B1 

Can some one please explain how this code works? in step by step...

trigger AddressFieldUpdateOnContacts on Account (after update) {

    set<ID> accountIDs = new set<ID>();
    
    for(account a : Trigger.new){
        accountIDs.add(a.ID);
    }
    List<Contact> contactList = [select ID, name, accountID from contact where accountID = :accountIDs];
    
    Account a = Trigger.new[0];
    for (contact c : contactList){
        c.MailingCity = a.BillingCity;
    }
    if(contactList.size()>0)
        update contactList;

sherod1sherod1

//Trigger runs after an account is updated

trigger AddressFieldUpdateOnContacts on Account (after update) {

   

         //Create a set to collect the unique id's of the accounts that are passed to the trigger.
    set<ID> accountIDs = new set<ID>();
    

         //For each of the accounts changed (up to 200 in one go, collect all the id's 
    for(account a : Trigger.new){
        accountIDs.add(a.ID);
    }

 

         //Now query the contact object for any contacts belonging to the Account that has been modified
    List<Contact> contactList = [select ID, name, accountID from contact where accountID = :accountIDs];
    

        //For only the FIRST account passed to the trigger
    Account a = Trigger.new[0];

 

       //Update all the contacts you found from all accounts with the billing city of the first account
    for (contact c : contactList){
        c.MailingCity = a.BillingCity;
    }

 

       //if there are contacts found, save them.
    if(contactList.size()>0)
        update contactList;

 

 

This code is very buggy, whilst it is attempting to handle bulkification (i.e more than one Account modified at a time, it makes a critical error and will update ALL contacts of ALL accounts modified with the BillingCity from the FIRST account passed in a list.   

karthiksaivkarthiksaiv

Requirement:

 Whenever an account record is updated, all the related contact's Mailing city should be updated wlth account Billing city.

According to me there is an error in the program, 

 

trigger AddressFieldUpdateOnContacts on Account (after update) 

{
    set<ID> accountIDs = new set<ID>();
    
    for(account a : Trigger.new)   // loop for updated account records

{
        accountIDs.add(a.ID);  // storing all the account Ids in a collection(set)
    }
    List<Contact> contactList = [select ID, name, accountID from contact where accountID = :accountIDs];  // retriving all the corresponding contact records present in the collection
    
    for(account a: trigger.new)  // loop for updated account records

{

    for (contact c : contactList)  // loops for retrived contact records

{

if(c.accountID==a.ID)  // checking for contact related to that account 
        c.MailingCity = a.BillingCity; // assigning cBilling city to mailing city 
    }

}
    if(contactList.size()>0)
        update contactList; // updating the list

}

B1B1

Thank you for your reply, 

 

I hope this will satisfy the bulkification, updating all accounts in one go...

===========================================================

trigger AddressFieldUpdateOnContacts on Account (after update) {

Set<ID> accountIDs = new set<ID>(); 
for(account a: Trigger.new){
accountIDs.add(a.ID); 

List<Contact> contactList = [select ID,name, accountID from contact where accountID = : accountIDs]; 

Map<ID,account> accMap = new Map<ID,Account>();
for(account a: Trigger.new){
accMap.put(a.ID,a); 
}
for(contact c :contactList){
c.MailingCity = accMap.get(c.accountID).BillingCity;



if(contactList.size() >0)
update contactList;
}

 

Can u please code me back, when updating a contact the account have to be updated(swaps) for all contacts........

B1B1

Provide me the code : when ever a contact address field is updated associated parent address have to updated.

 

Thanks and regards

Bhuvan

B1B1

Explain the code how the map helps to fetching the appropriate account id:

================================================================

trigger AddressFieldUpdateOnAccountToContact on Contact (before insert, after update) {

Set<ID> contactIDs = New Set<ID>();
for(contact c : Trigger.new){

ContactIDs.add(C.AccountID);
}

List<account> accountlist = [select ID, ParentID from account where ID = : contactIDs];
Map<ID,contact> contMap = new Map<ID,contact>();
for(contact c : Trigger.new){
contMap.put(c.AccountID, c);
}
for(Account a : accountlist){
a.BillingCity = contMap.get(a.ID).MailingCity;
}
update accountlist;
}

====================================================================