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
CKRCKR 

Need help for this trigger?

Hi All,
My current trigger avoids updating Status__c field on accounts from 'Open' to 'close' when both Account's status__c and its related Opportunities status__C is 'Open', Now,How can i use the same condition to avoid those accounts from being deleted(Before delete even which throws custom error message).
 my current condition in trigger is working for both before update and before delete events,but the before delete event in the trigger is not allowing me to delete any other records, those even didn't met the condition and i am seeing the follow error message instead i want to populate the custom error message.
  Help will be appreciated. 

User-added image
trigger AvoidStatusUpdateOnAcct on Account(before update,before delete) 
{  Set<Id> acctIdSet = new Set<Id>();   
    for (Account acct : Trigger.new)      
    {
        acctIdSet.Add(acct.Id);              
    }
    
    Map<Id, Account> acctMap = new Map<Id, Account>([SELECT Id,Name,Status__c, (SELECT Id FROM Opportunities WHERE Status__c = 'Open') FROM Account WHERE Id in :acctIdSet]);   
    for (Account acct : Trigger.new)
    {   List<Opportunity> oppList = acctMap.get(acct.Id).Opportunities;       
        if (acct.Status__c == 'Close' && oppList != null && oppList.size() >= 1)
        {
           acct.addError('Account ' + acct.Name + ' still has open opportunities.'); 
        }
    
    }
 }


Thanks
CKR    
RaidanRaidan
Hi CKR,

Delete trigger doesn't have a Trigger.new value but Trigger.old. You will have to branch out your code by checking Trigger.isUpdate and Trigger.isDelete.
Yury BondarauYury Bondarau
Hi CKR,

According to salesforce documentation in 'trigger on delete' context we can NOT access to Trigger.new. You can use Trigger.old instead.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

User-added image
CKRCKR
Hi All thanks for your time.
before posting my question here i tried like below,which did not worked as expected and even tried couple of other ways apart from the way the trigger has bee written below based on my knowledge
there were no compiling errors  (the below approach seem having repetitive code,not a proper way but i endup like this)
 
If (Trigger.IsBefore && Trigger.IsDelete)
Map<Id, Account> acctMap = new Map<Id, Account>([SELECT Id,Name,Status__c, (SELECT Id FROM Opportunities WHERE Status__c = 'Open') FROM Account WHERE Id in :acctIdSet]);   
    for (Account dacct : Trigger.Old)
    {   List<Opportunity> oppList = acctMap.get(acct.Id).Opportunities;       
        if (dacct.Status__c == 'Close' && oppList != null && oppList.size() >= 1)
        {
           dacct.addError('Account ' + acct.Name + ' still has open opportunities.'); 
        }

 
RaidanRaidan
You can create a method inside the trigger (see code below) to get rid of redundancy. However, this is not the best practice. The best way to approach this is to use a trigger handler class and call the methods from the trigger.
if (Trigger.isBefore && Trigger.isUpdate) {
	validateRecords(Trigger.new);
} else if (Trigger.isBefore && Trigger.isDelete) {
	validateRecords(Trigger.old);
}

private void validateRecords(List<Account>) {
	//do your validation here
}