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
Mr. WMr. W 

Trigger Help

Hi All,

 

I'm new to triggers and wanted to see if someone could help with my first basic one. 

 

Trigger Requirement:  I have a checkbox on Contacts.  If that is checked, then the contact's billing address should mirror the account's billing address.  So, if that box is checked, and someone changes the Account's billing address it automatically updates the contact's address as well.

 

(Note: I had a feild update in place but that updated only when the checkbox was updated, not if someone just updated the Account's billing addreess) 

 

Contact Fields:  Duplicate_Account_Address__c (checkbox), Mailing Street, Mailing City, Mailing State, Mailing Zip, Mailing Country

Account Fields: Billing Street, Billing City, Billing State, Billing Zip, Billing Country

 

 

Many Thanks! 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Two triggers:

 

trigger AccTrigger on Account (after update) {
    update [select id from contact where accountid in :trigger.new and duplicate_account_address__c = true];
}

 

trigger ConTrigger on Contact( before insert, before update ) {
   map< id, account > accs = new map< id, account >();
   for( contact c: trigger.new ) {
      if( c.duplicate_account_address__c && c.accountid != null ) {
         accs.put( c.accountid, null );
      }
   }
   accs.putAll( [select id, billingstreet, billingcity, billingstate, billingpostalcode, billingcountry from account where id in :accs.keyset()]);
   for( contact c: trigger.new ) {
      if( c.duplicate_account_address__c && c.accountid != null ) {
          c.mailingstreet = accs.get( c.accountid ).billingstreet;
          c.mailingcity = accs.get( c.accountid ).billingcity;
          c.mailingstate = accs.get( c.accountid ).billingstate;
          c.mailingpostalcode = accs.get( c.accountid ).billingpostalcode;
          c.mailingcountry = accs.get( c.accountid ).billingcountry;
      }
   }
}

The former updates all contacts that have the box checked when an account is changed, the latter copies the data if the box is checked. This will catch all possible conditions.

All Answers

sfdcfoxsfdcfox

Two triggers:

 

trigger AccTrigger on Account (after update) {
    update [select id from contact where accountid in :trigger.new and duplicate_account_address__c = true];
}

 

trigger ConTrigger on Contact( before insert, before update ) {
   map< id, account > accs = new map< id, account >();
   for( contact c: trigger.new ) {
      if( c.duplicate_account_address__c && c.accountid != null ) {
         accs.put( c.accountid, null );
      }
   }
   accs.putAll( [select id, billingstreet, billingcity, billingstate, billingpostalcode, billingcountry from account where id in :accs.keyset()]);
   for( contact c: trigger.new ) {
      if( c.duplicate_account_address__c && c.accountid != null ) {
          c.mailingstreet = accs.get( c.accountid ).billingstreet;
          c.mailingcity = accs.get( c.accountid ).billingcity;
          c.mailingstate = accs.get( c.accountid ).billingstate;
          c.mailingpostalcode = accs.get( c.accountid ).billingpostalcode;
          c.mailingcountry = accs.get( c.accountid ).billingcountry;
      }
   }
}

The former updates all contacts that have the box checked when an account is changed, the latter copies the data if the box is checked. This will catch all possible conditions.

This was selected as the best answer
Mr. WMr. W

You are an officer and a gentleman.  Thank you kindly.  It's working perfectly. 

 

I'm such a novice that I also need help writing the test class.  I'm looking around now, but if you could explain it easily that would be wonderful!

sfdcfoxsfdcfox
@istest
class TestTrigger {
  @istest
  static void test() {
    account a = new account(name='test',billingstreet='123 main');
    insert a;
    contact c = new contact(lastname='test',accountid=a.id,duplicate_account_address__c=true);
    insert c;
    c = [select id,accountid,mailingstreet from contact where id = :c.id];
// contact should have account address
    system.assertequals(a.billingstreet,c.mailingstreet);
    a.billingstreet='456 main';
    update a;
    c = [select id,accountid,mailingstreet from contact where id = :c.id];
// contact should still have account address
    system.assertequals(a.billingstreet,c.mailingstreet);
    c.duplicate_account_address__c=false;
    c.mailingstreet = '123 main';
    update c;
    c = [select id,accountid,mailingstreet from contact where id = :c.id];
// contact should no longer have account address
    system.assertnotequals(c.mailingstreet,a.billingstreet);
    a.mailingstreet = '987 main';
    update a;
    c = [select id,accountid,mailingstreet from contact where id = :c.id];
// contact should still not have account address
    system.assertnotequals(c.mailingstreet,a.billingstreet);
    c.duplicate_account_address__c=true;
    update c;
    c = [select id,accountid,mailingstreet from contact where id = :c.id];
// contact should now have account address
    system.assertequals(c.mailingstreet,a.billingstreet);
  }
}