• Vaquero95
  • 0 Points
  • Member since 2013

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 3

Hi team!


I have a requirement to prevent some users from deletion on tasks that are assigned to them.

SFDc support advised us to make a trigger to have that behavior.

When the trigger is fired, everyone has this error when they click on delete and I would like that Admins and 2 other Profiles could delete the tasks if needed.


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 "Apex trigger NoDeleteonIsAssignedTask caused an unexpected exception, contact your administrator: NoDeleteonIsAssignedTask: execution of BeforeDelete caused by: System.FinalException: Record is read-only: Trigger.NoDeleteonIsAssignedTask: line 6, column 1". Click here to return to the previous page.



My trigger :

trigger NoDeleteonIsAssignedTask on Task (before delete) 
String ProfileId = UserInfo.getProfileId();  
for (Task a : Trigger.old)      
IF(a.Is_Assignment__c=True &&(ProfileId!='00e20000000qb49' ||ProfileId !='00eP0000000M0nd'||ProfileId !='00e20000000ju28'))
     a.addError('You can\'t delete this record!');

 I would very much appreciate if someone could help me in writing this trigger on task before delete to prevent the users to deltee their tasks except for the 3 admin profiles .


Thanks in advance for your help.


Kind Regards,