+ Start a Discussion
Praveen JhaPraveen Jha 

​Email notification when an attachment is added to a case-Urgent

How can case owner and case team get a notification when an attachment is added to a case??
Best Answer chosen by Praveen Jha
Swati GSwati G
You need to define the type of list and set. Change code according to your requirement. Below code is for account attachment and according to ur requirement you want it for case

trigger AttachmentTriggerDemo on Attachment (before insert) {
    List<Account> accountList = new List<Account>();
    Set<Id>accIds = new Set<Id>();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Account.SobjectType){
              accIds.add(att.ParentId);
         }
    }
    accountList = [select id, has_Attachment__c from Account where id in : accIds];
    if(accountList!=null && accountList.size()>0){

        for(Account acc : accountList){
            acc.has_Attachment__c = true;
        }
        update accountList;
    }
}
 

All Answers

Swati GSwati G
You can add trigger in attachment as shown in below link

Attachmetn Trigger example: http://www.sfdcpoint.com/salesforce/trigger-on-attachment-in-salesforce/

and check if the parent id is case if yes, then get case owner by querying case and send mail through SingleEmailMessage.

SingleEmailMessage example: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_forcecom_email_outbound.htm 
Praveen JhaPraveen Jha
Hi Swati G thanks for your quick reply . When i save this code I am getting following error:-
Error: Compile Error: unexpected token: 'List' at line 2 column 4
Swati GSwati G
Counld you please share your code? so that I can see where are you getting error.
Praveen JhaPraveen Jha


trigger AttachmentTriggerDemo on Attachment (before insert) {
    List accountList = new List();
    Set accIds = new Set();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Account.SobjectType){
              accIds.add(att.ParentId);
         }
    }
    accountList = [select id, has_Attachment__c from Account where id in : accIds];
    if(accountList!=null && accountList.size()>0){
        for(Account acc : accountList){
            acc.has_Attachment__c = true;
        }
        update accountList;
    }
}
Swati GSwati G
You need to define the type of list and set. Change code according to your requirement. Below code is for account attachment and according to ur requirement you want it for case

trigger AttachmentTriggerDemo on Attachment (before insert) {
    List<Account> accountList = new List<Account>();
    Set<Id>accIds = new Set<Id>();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Account.SobjectType){
              accIds.add(att.ParentId);
         }
    }
    accountList = [select id, has_Attachment__c from Account where id in : accIds];
    if(accountList!=null && accountList.size()>0){

        for(Account acc : accountList){
            acc.has_Attachment__c = true;
        }
        update accountList;
    }
}
 
This was selected as the best answer
Praveen JhaPraveen Jha
Thanks Swati G for your great help.