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
jaishrijaishri 

Trigger on account to check the case is closed related account .If all case is closed then it will allow to save record on account with status Deceased. Help me

trigger AccountTrigger on Account (before update) {
    for (Account acc : Trigger.new) {
        if (acc.Status__c == 'Deceased' && (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 if (acc.Status__c == 'Deceased') {
            List<Case> relatedCases = [SELECT Id FROM Case WHERE AccountId = :acc.Id AND IsClosed = false];
            if (!relatedCases.isEmpty()) {
                acc.addError('Please close all related cases before marking the status as Deceased.');
            }
        }
    }
}

 
AshwiniAshwini (Salesforce Developers) 
Hi Jaishri,
Are you facing any errors with the logic that you have provided?

Thanks. 

Heads up about the Forums shut down

User-added image
Important Update

We appreciate your participation in these Salesforce Discussion Forums! It’s active members like you that keep our amazing community going strong.

At this time, we want to give you a heads up that on December 4, 2023, the discussion forums will shut down and all relevant discussions will migrate to the Trailblazer Community digital platform. This move brings all conversations around Salesforce development together in one place and provides more opportunities for our broader community to connect and share. We will be removing outdated, obsolete, or spam content and migrating only relevant discussions to the Trailblazer Community digital platform.

Starting November 20, you can view discussions but not post new questions or responses. On December 2, 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:
  1. If you’re not already a member of the Trailblazer Communitysign up for a Trailblazer account using the same login email address associated with your Developer Discussion Forums account. This is crucial.
  2. If you already have a Trailblazer account, and it’s using a different email address from the one you used for 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.
Once you’re in the Trailblazer Community, join the Migration Support Hub users group to help you navigate this transition.

We will keep you up to date throughout the transition, and we look forward to seeing you joining the developer discussions in the Trailblazer Community!

Sincerely,
The Forums Support Team
jaishrijaishri
hi Ashwini , I'm getting error while all the cases are closed related to particular account, after closing also I'm unable to save the account status as Deceased.
Pallavi Soni 32Pallavi Soni 32
Hi Jaishri,
Please try below code -
trigger AccountTrigger on Account (before update) {

 Set<Id> accountIds = new Set<Id>();
    for (Account acc :  Trigger.new) {
        accountIds.add(acc.Id);
    }

    Map<Id, Integer> openCaseCountMap = new Map<Id, Integer>();
    for (AggregateResult ar : [SELECT AccountId, COUNT(Id) caseCount
                              FROM Case
                              WHERE AccountId IN :accountIds AND Status != 'Closed'
                              GROUP BY AccountId]) {
        openCaseCountMap.put((Id)ar.get('AccountId'), (Integer)ar.get('caseCount'));
    }

    for (Account acc :  Trigger.new) {
        if (acc.Status__c == 'Deceased') {
            if (openCaseCountMap.containsKey(acc.Id) && openCaseCountMap.get(acc.Id) > 0) {
                acc.addError('Please close all related cases before marking the status as Deceased.');
            }
        }
    }
}
Hope this will help!
Thanks,
Pallavi