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
LudivineLudivine 

Trigger on Task before delete error

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,

Ludivine

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I think its this part of the check:

 

a.Is_Assignment__c=True

 

as this will be interpreted as an attempt to update the record - change this to use the '==' operator:

 

a.Is_Assignment__c==True

All Answers

bob_buzzardbob_buzzard

I think its this part of the check:

 

a.Is_Assignment__c=True

 

as this will be interpreted as an attempt to update the record - change this to use the '==' operator:

 

a.Is_Assignment__c==True
This was selected as the best answer
LudivineLudivine

Hi Bob!!

 

you're awfully good, now it's working!!

 

Thanks a lot for your prompt reply!

 

Kind regards,

Ludivine

Devendra@SFDCDevendra@SFDC

Hi,

 

Glad, your problem is solved now.

 

Only one suggestion here, please never use hardcoded id's, as id's can be different environments except Production and Full Copy sandbox.

 

For example you can use a query to find an Id of profile,

 

Profile objProfile = new Profile();

List<Profile> profileList = [Select Id from Profile where Name =: 'Test Profile'];

if(!profileList.isEmpty()){

    objProfile = profileList[0];

}

 

and further you can use objProfile.Id instead of hardcoded id's.

 

Hope this helps :)

 

Thanks,

Devendra

 

 

LudivineLudivine
Yes of course, I agree.
I have added your additional code in my trigger with the concerned profile
Names instead. It still works!
I have learnt a lot thanks to you today!
Thanks again!

Ludivine

______________________________________________________

Amcor - Creating a new world of packaging
______________________________________________________

CAUTION - This message may contain privileged and confidential information intended only for the use of the addressee named above. If you are not the intended recipient of this message you are hereby notified that any use, dissemination, distribution or reproduction of this message is prohibited. If you have received this message in error please notify Amcor immediately. Any views expressed in this message are those of the individual sender and may not necessarily reflect the views of Amcor.
Vaquero95Vaquero95

Hi,

I'm trying to do something similar where I want to restrict two profiles from being able to delete Tasks. However the trigger below restricts everyone, including System Admins. Anyone have a suggestion? Thanks!

 

 

trigger NoDeleteonTask on Task (before delete)
 {
String ProfileId = UserInfo.getProfileId();  
for (Task a : Trigger.old)      
            
IF(ProfileId!='00e40000000oz5j' || ProfileId !='00e40000000ozYh')
{
     a.addError('You can\'t delete this record!');
     
            }
        }

bob_buzzardbob_buzzard

Your code looks to me like it will only allow two profiles to delete those tasks - your condition is if it doesn't equal profile 1 or profile 2, throw an error.  Presumably sysadmin doesn't equal either of those ids so thatwould be disallowed.

 

Using ids for profiles isn't a great idea as if they are custom profiles they are subject to change when moving between environments.

 

A better way to do this would be to retrieve the profiles by name, and then check them. Something like:

 

List<Profile> profiles=[select id from Profile where name='Profile 1' or name='Profile 2'];

if (2!=profiles.size())
{
   // unable to get the profiles - handle error
}
else
{
   if ( (profileId==profiles[0].id) || (profileId==profiles[1].id) )
   {
      a.addError('You can\'t delete this record');
   }
}

 Obviously repace Profile 1 and Profile 2 with the real profile names.

 

Vaquero95Vaquero95

Thanks, Bob. I tried the below and got this error: Compile Error: Variable does not exist: profileId at line 11 column 10

I also tried using profileid! to no avail... what do you think? Thanks so much!

 

 

trigger NoDeleteonTask on Task (before delete)
{


List<Profile> profiles=[select id from Profile where name='Custom - MDS' or name='Custom MDS - InsideView'];

if (2!=profiles.size())
{
   // unable to get the profiles - handle error
}
else
{
   if ( (profileId==profiles[0].id) || (profileId==profiles[1].id) )
   {
      a.addError('You can\'t delete this record');
   }
}}

bob_buzzardbob_buzzard

My code was a snippet to replace the code inside your loop, not the whole trigger :)

bob_buzzardbob_buzzard
trigger NoDeleteonTask on Task (before delete)
{
   String ProfileId = UserInfo.getProfileId();  
   List<Profile> profiles=[select id from Profile where name='Profile 1' or name='Profile 2'];

   if (2!=profiles.size())
   {
      // unable to get the profiles - handle error
   }
   else
   {
       for (Task a : Trigger.old)      
       {            
          if ( (profileId==profiles[0].id) || (profileId==profiles[1].id) )
          {
             a.addError('You can\'t delete this record');
          }
       }            
   }
}

 

Vaquero95Vaquero95

You rock! Thanks again.

sathishsfdcsathishsfdc
Hi . this error message is firing on a different page.....

something like :

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 can't delete this record"

is there any workaround for this???.