You need to sign in to do that
Don't have an account?
jaishri
Trigger will execute on specific fields (Status ,DOB , DOD ) change on account record. Trigger is executing while updating other fields also. please help in code
public class AccountTriggerHelper { public static void validateDeceasedAccounts(List<Account> newAccounts, Map<Id, Account> oldMap) { Set<Id> accIdSet = new Set<Id>(); Map<Id, Case> casesMap = new Map<Id, Case>(); for (Account acc : newAccounts) { Account oldAcc = oldMap.get(acc.Id); if (acc.Status__c == 'Deceased' && ( acc.DOB__c != oldAcc.DOB__c || acc.DOD__c != oldAcc.DOD__c || acc.Status__c != oldAcc.Status__c )) { if (acc.DOB__c == null || acc.DOD__c == null || acc.DOD__c <= acc.DOB__c) { acc.addError('Please ensure that Date of Birth is populated, Date of Death is greater than Date of Birth, and Status is set to Deceased.'); } else { accIdSet.add(acc.Id); } } } if (!accIdSet.isEmpty()) { for (Case c : [SELECT Id, AccountId FROM Case WHERE AccountId IN :accIdSet AND Status != 'Closed']) { casesMap.put(c.AccountId, c); } } for (Account acc : newAccounts) { if (acc.Status__c == 'Deceased' && casesMap.containsKey(acc.Id)) { acc.addError('Please close all related cases before marking the status as Deceased.'); } } } }
- For this, you can reference the Trigger.oldMap and Trigger.newMap map collections. So, you can compare old values with new values.
- So, the Trigger will always 'fire' but you will only act upon it if the new and old values are different
Check the reference below from Stack Exchange.https://salesforce.stackexchange.com/questions/11191/just-update-when-a-specific-field-changes
Please mark it as Best Answer if the above information was helpful.
Thanks.
Important Update - what you need to do
As a reminder, on December 4, 2023 the Salesforce Discussion Forums will be removed from the Salesforce Developers website. These forums will shut down and all relevant discussions will migrate to the Trailblazer Community digital platform.
During the week starting November 20, you can view discussions, but not post new questions or responses. On December 4, you will no longer be able to access the discussion forums from the Salesforce Developers website.
Please take these steps before November 30, 2023, 11:59 p.m. PT to ensure your contributions follow you to the Trailblazer Community:
- If you’re not already a member of the Trailblazer Community, sign up for a Trailblazer account using the same login email address associated with your Developer Discussion Forums account. This is crucial.
- If you already have a Trailblazer account, and it’s using a different email address from your Developer Discussion Forums account, we recommend that you log in to the Trailblazer Community and connect your forums email address to your Trailblazer account.
Find more info & supportWe know that moving platforms can be a hassle, so we created a special forums users group, the Migration Support Hub, in the Trailblazer Community where you can get training videos, information, and assistance.
We are here to help you through this transition!
Sincerely,
The Forums Support Team