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
Simon Cremoni 4Simon 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 :
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 !
 
Best Answer chosen by Simon Cremoni 4
Ajay K DubediAjay K Dubedi
So you may need to use undelete operation in after delete Trigger

All Answers

Prakash NawalePrakash Nawale
Hi Simon Cremoni 4,
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

 
Simon Cremoni 4Simon Cremoni 4
Thanks Prakash,
but is there any workaround.
I tested @future methode and batch without succes...
Ajay K DubediAjay K Dubedi
Hi Simon Cremoni
Try this
for(Event event1:trigger.old){
	 for(Event e :EventToCancels)
		{
			if(event1.id==e.id)	
				event1.adderror('Call can\'t be removed, it was cancelled.');
		}
	}

 
Simon Cremoni 4Simon Cremoni 4
Thanks Ajay,
I tried it, the the deletion is blocked and the message appear but it don't do the update :(
Ajay K DubediAjay K Dubedi
    HI Simon Cremoni
    So you have to fetch those events which  and then update it.
    ​So try this.
set<id> Event_ids=new Set<id>();
		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'));
			Event_ids.add(e.id);
			
		}
	}
	
	list<Event> update_event=new List<Event>();
	update_event=[select Status__c,ReasonForCancellation__c from event where ID IN:Event_ids];
	
	for(Event ee:update_event){
		
		ee.Status__c='Cancelled';
		ee.ReasonForCancellation__c='Created By Mistake';
		
	}update update_event;

 
Simon Cremoni 4Simon Cremoni 4
Thanks Ajay,
I tried it but unfortunatly it don't update when I let the "Adderror" part.
Here the code I tried :
List<Event>EventToCancels = new List<Event>();
	List<Event>EventToUpdate = new List<Event>();
	set<id> Event_ids=new Set<id>();
	
	for(Event e :trigger.old)
	{
		if(e.Type__c == 'Call' /*&& UserInfo.getProfileId() != Label.AdminProfileId*/ && curUser.BYPASSVALIDATIONRULE__c == false)
		{
			EventToCancels.add(e);
			Event_ids.add(e.id);
			//EventToUpdate.add(new Event(Id=e.Id,Status__c='Cancelled',ReasonForCancellation__c='Created By Mistake'));
			
		}
	}

	if(Event_ids.size()>0)
	{
		EventToUpdate = [select Status__c,ReasonForCancellation__c from event where ID IN:Event_ids];	
		for(Event ee :EventToUpdate)
		{
			ee.Status__c='Cancelled';
			ee.ReasonForCancellation__c='Created By Mistake';
		}
		update EventToUpdate;		
	}



	for(Event e :EventToCancels)
	{
		e.adderror('Call can\'t be removed, it was cancelled.');
	}

 
Ajay K DubediAjay K Dubedi
So you may need to use undelete operation in after delete Trigger
This was selected as the best answer
Simon Cremoni 4Simon Cremoni 4
Thanks it work:
trigger EventAfterDelete on Event (after delete) {
	User curUser = [Select Id, BYPASSVALIDATIONRULE__c From USER Where Id = :UserInfo.getUserId()];
	set<id> Event_ids=new Set<id>();
	for(Event e :trigger.old)
	{
		if(e.Type__c == 'Call' /*&& UserInfo.getProfileId() != Label.AdminProfileId*/ && curUser.BYPASSVALIDATIONRULE__c == false)
		{
			Event_ids.add(e.id);
		}
	}

	if(Event_ids.size()>0)
	{
		AP02Event.CancelEventFuture(Event_ids);
	}
}
 
@future
	public static void CancelEventFuture(Set<Id> EventIds)
	{
		system.debug('## CancelEventFuture Start' );	
		List<Event>EventsToCancel = new List<Event>();
		for(id eId :EventIds)
		{
			EventsToCancel.add(new Event(Id=eId,Status__c='Cancelled',	ReasonForCancellation__c='Created By Mistake'));
		}
		undelete EventsToCancel;
		update EventsToCancel;
	}