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
Jason WenigJason Wenig 

Outlook Integration and Delete Trigger issue when associating with Opp or Acct

My org uses a Detete trigger to prevent the Salespeople from removing tasks assigned to them.  Somehow the following delete trigger on the Task is being called during the association of an Email via Outlook to an Account or Opportunity.  The trigger is not called when associated to a Contact.  I'm not sure how to update the trigger to get around this issue.

trigger NoDeleteonTask on Task (before delete)
{
   String ProfileId = UserInfo.getProfileId();  
   List<Profile> profiles=[select id from Profile where name='TJC Business Development User' or name='TJC Business Development User No Transfer'];

   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('Business Development profiles cannot delete tasks');
          }
       }            
   }
Gwenn Ferguson 17Gwenn Ferguson 17
Jason - Have you discovered anything on this?

We are having a similar issue trying to log an email to an Opportunity record using the Outlook Integration. When the email has an attachment the following error is returned:
The apex class Messaging.EmailToSalesforceHandler failed due to: System.UnexpectedException: Task cannot be deleted, please contact your system administrator.
---------------
The attached email could not be processed because the Apex class Messaging.EmailToSalesforceHandler failed.
System.UnexpectedException: Task cannot be deleted, please contact your system administrator.

We have an Trigger that prohibits any email/event/task/file from being deleted. The trigger prohibits a task/email/event/file from being deleted however using the integration we are trying to attach a file(s) to the task that is being created using the Integration.

Apex Class details:
/*  This class is used to prevent the deletion of an email. The only case in which objects can be deleted
    are if they are specified in the DeletePermissions__c custom settings.
*/
public class EmailMessageTriggerHandler {

    public static void onBeforeDelete(List<EmailMessage> eventList) {

        try {
            DeletePermissions__c permissions = [SELECT SetupOwnerId, Delete_Emails__c, Delete_Events__c, Delete_Files__c, Delete_Tasks__c FROM DeletePermissions__c LIMIT 1];
            if(UserInfo.getProfileId() == permissions.SetupOwnerId && permissions.Delete_Emails__c == true){
                //Objects may be deleted under this condition
            }else{
                for (EmailMessage e : eventList) {
                    e.addError('Email cannot be deleted, please contact your system administrator.');
                }
            }      
        } catch (Exception ex) {
            System.debug('Custom Settings: "Delete Files, Tasks, and Events" must be set');
            for (EmailMessage e : eventList) {
                e.addError('Email cannot be deleted, please contact your system administrator.');
            }
        }
        
    }

}


 
Bond GrigsbyBond Grigsby
I totally like your gave limits as the post you passed on has some uncommon information which is totally essential for me.
DG Paystub (https://www.dgpaystub.net/)