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 

How to reference fields on What parent object in Task Trigger

Hello, please see Event trigger below. It prevents the delete of an Event record if running user is not equal to created by user. I would like to update to add another condition to the IF statement ...and if Opportunity.Stage != 'Appt Canceled'. Perhaps something along the lines of if ((UserInfo.getUserId() != a.CreatedById)) && a.what.stage != 'Appt Cancelled')

However, I know that a.what.stage = 'Appt Cancelled' is not the right way to do this due to the polymorphic nature of the What relationship field. How can I write this in so that Salesforce knows I want to look only at Opportunity.Stage? 

Below is the full trigger. Thank you to anyone who has a few minutes to help. 

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');

          }
       }

}
RAM AnisettiRAM Anisetti
Hi ,
try this one.....
 
trigger ramtri on Event (before delete) {

Map<ID,String>mapval=new Map<ID,String>();
List<ID>ids=new List<ID>();
for(Event e:trigger.old){

if(e.whatid!=null){
  ids.add(e.whatid);
}

}


for(Opportunity op:[select id,StageName from Opportunity where id IN:ids]){
  mapval.put(op.id,op.StageName);
}


for(Event e1:trigger.old){

String str=mapval.get(e1.whatid);

if (UserInfo.getUserId()!= e1.CreatedById && str != 'Appt Cancelled'){

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

}

}

 
sslodhi87sslodhi87
trigger NoDeleteonEvent on Event (before delete)
{
    Set<Id> setOpportunityId = new Set<Id>();
    for (Event objEvent : Trigger.old)     
    {    
        if(objEvent.WhatId != null && Schema.Opportunity.SObjectType == objEvent.WhatId.getSobjectType())
            setOpportunityId.add(objEvent.WhatId);
        
    }
    Map<Id, Opportunity> mapIdToOpportunity = new Map<Id, Opportunity>([SELECT Id, StageName FROM Opportunity WHERE Id IN: setOpportunityId]);
    
    for (Event objEvent : Trigger.old)     
    {    
        if(UserInfo.getUserId()!= e1.CreatedById && objEvent.WhatId != null && mapIdToOpportunity.containsKey(objEvent.WhatId) && mapIdToOpportunity.get(objEvent.WhatId).StageName != 'Appt Cancelled')
        {
            objEvent.addError('You cannot delete an event that was assigned to you by another person');

        }    
        
    }
    
}
Stephanie Zimmerman 7Stephanie Zimmerman 7
Hi Ram, The trigger worked as need in the sandbox but not in production. I just refreshed the sandbox so it's not a matter of a difference between the two. Do you think it could be a problem of using a before trigger and getting stuck in a recursive loop? I'm not sure that anything but a before trigger could be possible here. Stephanie