You need to sign in to do that
Don't have an account?
Simon Cremoni 4
Update record on before delete trigger
Hi,
I created a trigger before delete.
The objective is to block the deletion and update the record (event) to set the status to 'Cancelled'.
I don't have error but the update don't take effect.
Can you help me to find what's wrong ?
Here my trigger :
I created a trigger before delete.
The objective is to block the deletion and update the record (event) to set the status to 'Cancelled'.
I don't have error but the update don't take effect.
Can you help me to find what's wrong ?
Here my trigger :
trigger EventBeforeDelete on Event (before delete) { system.debug('## EventBeforeDelete'); User curUser = [Select Id, BYPASSVALIDATIONRULE__c From USER Where Id = :UserInfo.getUserId()]; List<Event>EventToCancels = new List<Event>(); List<Event>EventToUpdate = new List<Event>(); for(Event e :trigger.old) { if(e.Type__c == 'Call' /*&& UserInfo.getProfileId() != Label.AdminProfileId*/ && curUser.BYPASSVALIDATIONRULE__c == false) { EventToCancels.add(e); EventToUpdate.add(new Event(Id=e.Id,Status__c='Cancelled',ReasonForCancellation__c='Created By Mistake')); } } if(EventToUpdate.size()>0) { update EventToUpdate; } for(Event e :EventToCancels) { e.adderror('Call can\'t be removed, it was cancelled.'); } }Thanks for your help !
All Answers
You can't do update on same record in before delete.
FYI : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm
but is there any workaround.
I tested @future methode and batch without succes...
Try this
I tried it, the the deletion is blocked and the message appear but it don't do the update :(
So you have to fetch those events which and then update it.
So try this.
I tried it but unfortunatly it don't update when I let the "Adderror" part.
Here the code I tried :