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
speedforcespeedforce 

Trigger to update fields upon updates to formula fields in Contacts and Accounts

I am very new to Salesforce and I need to write a trigger which should fire on few formula field updates and should update the other fields in contacts and accounts. So something like this:

Trigger should fire when updates are done to these fields:
From Contact object fields - ED_180_Prior__c (formula date field), Prof_Status__c (text)
From Account object fields - M_Status__c (formula text field), Days_of_MT__c (formula number field).

Trigger should update these fields:
From Contact object - Pardot_ED_180_Prior__c (date field), Pardot_Prof_Status__c (text).
From Accounts object - Pardot_M_status__c (text field), Pardot_Days_of_MT__c (number field)

I have tried this so far:

    trigger updatePardot on Contact (after insert)
    {
        List<contact> conUpdate = new List<Contact>();
        set<Id> sAccId = new set<Id>();
        for(Contact con: trigger.new) {
                sAccId.add(con.AccountId);
        }
        List<Account> lstAccount = [select id, Address_1__c, (select id,Address_1__c from contacts) from account where id IN: sAccId];
        for(Account acc: lstAccount) {
                for(Contact con: acc.contacts) {
                       //con.Address_1__c = acc.Address_1__c;
                       con.Pardot_ED_180_Prior__c = con.ED_180_Prior__c;
                       con.Pardot_Prof_Status__c = con.Prof_Status__c;
                       conUpdate.upsert(con);

                       acc.Pardot_M_status__c = acc.M_status__c;
                       acc.Pardot_Days_of_MT__c = acc.Days_of_MT__c;
                       lstAccount.upsert(acc);
           }
       }
   }

Can someone please help me if I am not doing it correct, or if I should change the approach as soon as possible? Also, should I setup workflow rules for every different field when this trigger should fire? Much thanks in advance.
JitukawaleJitukawale
Hi Speedforce,

Concentrate on below points:

1. Formula fields never update. Formulas are calculated when it is viewed or queried.
2. Use "Before update" trigger event for the trigger.
3. Because formula fields never update, use fields that used in formula fields.
4. See below code for reference and focus on code comments.
     
// use before update event
 trigger updatePardot on Contact (before update)
    {
        if(Trigger.isUpdate && Trigger.isBefore) {
			List<contact> conUpdate = new List<Contact>();
			set<Id> sAccId = new set<Id>();
			for(Contact con: trigger.new) {
					// check if value is changed from old values. Add other criteria
					if(con.Prof_Status__c != trigger.oldMap.get(con.Id).Prof_Status__c )
						sAccId.add(con.AccountId);
			}
			List<Account> lstAccount = [select id, Address_1__c, (select id,Address_1__c from contacts) from account where id IN: sAccId];
			for(Account acc: lstAccount) {
					// your field update code here.
		   }
		}
   }

Mark this as the best answer if this helps you.

Regards,
jitendra Kawale.