23.6k views
1 vote
What is the declarative solution for the following scenario:

A custom field on related Contact records needs to be updated whenever an account is updated

User Ohad Perry
by
8.3k points

1 Answer

4 votes

Final answer:

The declarative solution for this scenario is to use a trigger in Salesforce. By using an After Update trigger on the Account object, you can update the custom field on related Contact records.

Step-by-step explanation:

The declarative solution for this scenario would be to use a trigger in Salesforce. A trigger is a piece of code that is executed before or after a record is inserted, updated, or deleted. In this case, we can use an After Update trigger on the Account object.

The trigger code would check if any related Contacts exist for the updated Account and update the custom field on those Contacts accordingly. This can be achieved by querying the Contact records related to the updated Account and updating the custom field using DML statements.

Here is an example of how the trigger code might look in Apex:

trigger UpdateContactCustomField on Account (after update) {
Set<Id> accountIds = new Set<Id>();
for (Account acc : Trigger.new) {
accountIds.add(acc.Id);
}
List<Contact> contactsToUpdate = [SELECT Id, CustomField__c FROM Contact WHERE AccountId IN :accountIds];
for (Contact con : contactsToUpdate) {
con.CustomField__c = 'Updated Value';
}
update contactsToUpdate;
}

User Ddk
by
8.9k points