You need to sign in to do that
Don't have an account?
CKR
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.
Thanks
CKR
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.
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
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.
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
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)