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
bookerawardbookeraward 

Fire a Trigger

Hello all,
I am new to Salesforce.Can anyone please help with this problem.
 I am trying to Build a trigger.The trigger should fire whenever an Account object is modified.The trigger should detect if the billing address on the Account has changed.If the Account billing address has changed, the new billing address should be copied to all Contact records related to that account
Best Answer chosen by bookeraward
Shailesh DeshpandeShailesh Deshpande
Hi Faiza,

Below code should work for you:
 
trigger UpdateContactBillingAddress on Account (after update) {

    Set<Id> accountId = new Set<Id>();
    List<Contact> contactsToUpdate = new List<Contact>();
    
    for(Account acc: Trigger.new){

        // Please perform a check to see if address was updated

        accountId.add(acc.Id); //accounts that were updated.
    }
    
    for(Contact c : [Select Id, MailingCity, MailingStreet, MailingCountry, 
                            Account.BillingCity, Account.BillingStreet, Account.BillingCountry 
                      from Contact where Account.Id in: accountId]){

                 c.MailingCity = c.Account.MailingCity;
                 ///same for rest of the fields
                 
                 contactsToUpdate.add(c);
    }

    update contactsToUpdate;
    
}

BTW, i guess you can also achieve this via process builder instead of a trigger.

Thanks,
Shailesh.

All Answers

Shailesh DeshpandeShailesh Deshpande
Hi Faiza,

Below code should work for you:
 
trigger UpdateContactBillingAddress on Account (after update) {

    Set<Id> accountId = new Set<Id>();
    List<Contact> contactsToUpdate = new List<Contact>();
    
    for(Account acc: Trigger.new){

        // Please perform a check to see if address was updated

        accountId.add(acc.Id); //accounts that were updated.
    }
    
    for(Contact c : [Select Id, MailingCity, MailingStreet, MailingCountry, 
                            Account.BillingCity, Account.BillingStreet, Account.BillingCountry 
                      from Contact where Account.Id in: accountId]){

                 c.MailingCity = c.Account.MailingCity;
                 ///same for rest of the fields
                 
                 contactsToUpdate.add(c);
    }

    update contactsToUpdate;
    
}

BTW, i guess you can also achieve this via process builder instead of a trigger.

Thanks,
Shailesh.
This was selected as the best answer
Himanshu ParasharHimanshu Parashar
Hi Faiza,

You can use following code to achieve that.

 
trigger AccountAddressTrigger on Account (afte update) {

     set<ID> UpdatedAccountIds = set<ID>();
    List<Contact> lstUpdatedConact = set<Contact>();\
     //Get all the updated account ids.
     for(Account a : Trigger.new)
     {
             if(a.Billing_Address__c!=Trigger.oldmap.get(a.id).Billing_Address__c)
                  UpdatedAccountIds.add(a.id);
     }

   //Related Contact records
   List<Contact> lstUpdatedConact = [select id,Address__c from contact where AccountId in UpdatedAccountIds];
   for(Contact con : lstUpdatedConact )
   {
          con.Address__c= Trigger.newmap.get(AccountId).Billing_Address__c)              
   }

  if(lstUpdatedConact.size()>0)
     update lstUpdatedConact;

}

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
Sai Ram ASai Ram A
Hello Faiza Qadri

We can achieve this by Lightning Process Builder
Follow below steps to create a process to update If Account Billing Address ISCHANGED to Associated Contacts 

Step1: steup > Create > Workflows&Approvals > Process Builder >New > Name your Process
User-added image
Step2: Choose Object and Specify When to Start the Process > Account & Start the process > when a record is created or edited
User-added image
Step3: Define Criteria for this Action Group > Criteria Name > Is Billing Address Changed & Criteria for Executing Actions > (Select)Filter conditions are met. Next Set Filter Conditions as shown in screenshot
User-added image
Step4: IMMEDIATE ACTIONS > update Records > Action Name > Update Billing Address > Object (SELECT==> [Account].Contacts) as shown in screen shot & Set Object Variables (Field mapping)
User-added image
Step5: Acitvate the Process

If my answer helps resolve your query, please mark it as the 'Best Answer' to benefit others and improve the overall quality of Discussion Forums.

Thank you
BLearn - Sai
bookerawardbookeraward
Hello Shailesh,

Thanks.
I am getting an error
Compile Error: Invalid field MailingCity for SObject Account at line 17 column 34.
Any Insights.
Shailesh DeshpandeShailesh Deshpande
It should c.Account.BillingCity instead of c.Account.MailingCity. It was a typo.


Thanks,
Shailesh.
bookerawardbookeraward
Can you please show me how to write the test trigger for this trigger.