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
Stephanie Zimmerman 7Stephanie Zimmerman 7 

Preventing Delete of Event when Running User is Not Equal to Created By

Hi, I have what I think should be a very simple trigger that is intended to prevent users from deleting Events unless they are the user that created it. 

This is the code:
trigger NoDeleteonEvent on Event (before delete)
{

       
       for (Event a : Trigger.old)     

       {           

          if (UserInfo.getUserId() != a.CreatedBy.Id)

          {

             a.addError('You cannot delete an event that was assigned to you by another person');

          }
       }

}

The problem is that the code prevents all users from deleting Events, even if they are the CreatedBy user. So I assume the problem is with this line ( if (UserInfo.getUserId() != a.CreatedBy.Id) ), though I'm not sure what the problem with it is. 

Any advice is much appreciated. Thank you!
Best Answer chosen by Stephanie Zimmerman 7
Muthuraj TMuthuraj T
trigger NoDeleteonEvent on Event (before delete)
{

       
       for (Event a : Trigger.old)     

       {           
          system.debug(UserInfo.getUserId()+ ' -- ' +a.CreatedById);
          if (UserInfo.getUserId() != a.CreatedById)

          {

             a.addError('You cannot delete an event that was assigned to you by another person');

          }
       }

}

Use CreatedById instead of CreatedBy.Id . The above code should work.

All Answers

Muthuraj TMuthuraj T
trigger NoDeleteonEvent on Event (before delete)
{

       
       for (Event a : Trigger.old)     

       {           
          system.debug(UserInfo.getUserId()+ ' -- ' +a.CreatedById);
          if (UserInfo.getUserId() != a.CreatedById)

          {

             a.addError('You cannot delete an event that was assigned to you by another person');

          }
       }

}

Use CreatedById instead of CreatedBy.Id . The above code should work.
This was selected as the best answer
Stephanie Zimmerman 7Stephanie Zimmerman 7
That fixed it--thank you!!
Stephanie Zimmerman 7Stephanie Zimmerman 7
Also, Muthuraj, can you advise me on the test class? How can I test if the createdbyid is different than the running userid since createdby is not an editable field? For instance, when inserting an Event, I cannot set CreatedBy. Thank you
Hera SoherwardyHera Soherwardy
Hello, I have a similar issue where I don't want certain profiles to delete events. This code only allows admins to delete, any way to expand that group of users?



<apex:page action="{!if($User.Alias !='hsohe' && $User.Alias != 'c62' && $User.Alias != 'brendon',
null,
urlFor($Action.Event.Delete, $CurrentPage.Parameters.id, [retURL='/00T'], true) ) }"
standardController="Event">
<apex:pageBlock >
<apex:PageMessage summary="You are not allowed to delete Events" severity="Warning" strength="3"/>
<apex:pageMessages /> </apex:pageBlock> </apex:page>