+ Start a Discussion

after delete trigger on task throwing validation error

i have created a trigger on task so that task records cannot be deleted for a particular profile/record type:

trigger taskDelete on Task (before delete)
      system.debug('inside taskdelete');
      Id taskId;
      ID shipperRecordType = [SELECT Id FROM RecordType WHERE Name = 'Interaction'].Id;
       for(task a:Trigger.old)
          if(shipperRecordType == a.recordTypeId)
            system.debug('a.recordTypeId' + a.recordTypeId);
            system.debug('shipperRecordType'+ shipperRecordType);
            a.addErro.('you cannot delete this task');

the trigger is working properly but the error message is coming like this on the next page:

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "you cannot delete this task".
Is there any workaround for this???

The error is being displayed properly given that you are simply adding an error to the record before a transaction occurs and Salesforce is displaying it using the standard styles. Because you have added an error to the task the system cannot save it as it will not pass the validation requirements of having no errors. If you want to display the error message in a different format you need to override the page using Visualforce to display a custom error message styled as you would like.
Hi All,thanks for the reply....there is no Custom validation rule present in my ORG...
is there any example which can be helpful??

The validation rule is that the object cannot have any errors on it when it is being saved. You have a line of code:
a.addError.('you cannot delete this task');
which is adding an error to the object. When the system tries to save this object it will have a validation error because you have added the error. Because you have defined a custom error it returns as a custom validation issue.

Thanks for the reply again....if i remove the error message then the task is getting deleted......is there any way to show the error message without the addError() method???

Sadly not - the best you could try and do is to override the page as I mentioned above to display the error differently. Note that it is a best practice to display an error message for a user when they have performed an illegal action (i.e. deleting the task) as it educates them about their future interaction with the system to avoid errors and undesirable behaviour going forward.
Hi. I would like to override the standard page that is displayed by validation errors on a delete trigger. You mention this, but do you know how you can do it? I can not find where it is defined or linked to.
Purvi Gupchup 2Purvi Gupchup 2
Hi,I am having similar issue as mentioned above with one of my trigger.But I am not supposed to ovverride the standard page with the custom vf page.What could be the possible way to achieve this?