+ Start a Discussion
Gallery IntegrationGallery Integration 

Email Notification when uploading Attachment on Task

Hi Everyone,

I have been trying to create an email notiication to task assignee upon a new attachment is uploaded to the task.
I created a checkbox field in activites called "Send_Attachment_Email__c".
If this checkbox is true, then a workflow to fire the email notification will be run.

However, I have a problem in the trigger to check the checkbox to true when an attachment is uploaded to task.
Can anybody help me?
 
trigger attachmentTrig on Attachment (after insert) {
  // 1. Get List of Object Ids to notify on
    Set<Id> objectIds = new Set<Id>();

     
    // 2. Loop through list of attachments on Task
    for(Attachment a:trigger.new){

         String keyPrefix = a.ParentId;
         if(keyPrefix == 'Task'){
            objectIds.add(a.ParentId);
         }
    }

     
    // 3. Set the Send Attachment Notification Email field to True
    if(objectIds.size() > 0){

        List<Task> TaskList = [SELECT Id FROM Task WHERE Id IN :objectIds];
         
        for(Task obj:TaskList){
            obj.Send_Attachment_Email__c = true;
        }
      
        if(TaskList.size() > 0){
            update TaskList;
        }      
    }    
}

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
Use this code:
 
trigger attachmentTrig on Attachment (after insert) {
  // 1. Get List of Object Ids to notify on
    Set<Id> objectIds = new Set<Id>();

     
    // 2. Loop through list of attachments on Task
    for(Attachment a:trigger.new){

         String keyPrefix = a.ParentId;
         if(keyPrefix.startsWith('00T')){
            objectIds.add(a.ParentId);
         }
    }

     
    // 3. Set the Send Attachment Notification Email field to True
    if(objectIds.size() > 0){

        List<Task> TaskList = [SELECT Id FROM Task WHERE Id IN :objectIds];
         
        for(Task obj:TaskList){
            obj.Send_Attachment_Email__c = true;
        }
      
        if(TaskList.size() > 0){
            update TaskList;
        }      
    }    
}

Let me know if it helps
Thanks!
Gallery IntegrationGallery Integration
Hi Nads,

Thanks for your help.
I have tried the code you gave (changing the if to '00T').
However it still dont work.

The checkbox is still not checked when a file is uploaded on task.
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Finally after a lot of digging, I managed to find a solution:

First create this class in your org.
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Now, delete/deactivate the attachment trigger which you had created earlier.

And now create a trigger on Task object as follows:
trigger TaskTrig on Task (after insert, after update) {
    
    task[] TaskListToupdate=new task[]{};
    set<ID> TaskIDs=new set<id>();
    if(trigger.isinsert){
        
        for(task t:trigger.new){
        	taskIDS.add(t.id);
        }
        attachment[] attachmentlist=[select parentid from attachment where parentId in :TaskIDs];
        
        if(attachmentlist.size()>0){
            task[] Tasklist=[select id,Send_Attachment_Email__c from task where id in :TaskIDs];
            
            for(Task t:Tasklist ){
                for(attachment a: attachmentlist){
                    if(a.parentID==t.id){
                		t.Send_Attachment_Email__c=true;
                        TaskListToupdate.add(t);
                    }
        		}
            }
        }
        if(TaskListToupdate.size()>0)
        	update TaskListToupdate;
     }
    
    if(trigger.isupdate){
        if(checkrecursive.runOnce()){
            for(task t:trigger.new){
        		taskIDS.add(t.id);
        	}
            attachment[] attachmentlist=[select parentid from attachment where parentId in :TaskIDs];
            task[] Tasklist=[select id,Send_Attachment_Email__c from task where id in :TaskIDs];
            for(Task t:Tasklist){
                for(attachment a: attachmentlist){
                    if(a.parentID==t.id){
                		t.Send_Attachment_Email__c=true;
                        TaskListToupdate.add(t);
                    }
        		}
            }
            if(TaskListToupdate.size()>0)
        	update TaskListToupdate;
        }  
    }
    
}

I will explain why and what about the code, but let's first get your requirement fulfilled.
 
Gallery IntegrationGallery Integration
Hi Nads,

Really Appreciate your help here.
However, after trying your code, the send_attachment_email__c checkbox is also not checked even after i uploaded the attachment on task.
Narender Singh(Nads)Narender Singh(Nads)
You might be missing something because I only sent you that code after testing the code on me org.

Did you deactivate the attachment trigger?
Also, what i would like to know is from where are you adding the attachment to the task. Please share the screenshot.
Gallery IntegrationGallery Integration
Hi,

Yes I already deactivate the trigger on attachment,
Here is the screenshot on where I upload the attachment:

User-added image