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
aparna d 1aparna d 1 

Email Notification to case Team Members

Hi All,

How to send email notification to all case team members using triggers. I have requirement  location object is child and case is parent. when status field  is compleate on location object then send email to all case team members, Please any one give me solution
Best Answer chosen by aparna d 1
sowmya Inturi 9sowmya Inturi 9
Hi Aparna,

If the relationship between case and Location object is master detail, You can create a Roll Up summary field on Case which counts all the Location object records which has status 'Completed' . Based on the count (count>0), Create a process builder to send the email to Case Team Members.
--- Let me know if you need more clarification.

If the relationship id Lookup, You can create a trigger as below:

trigger EmailToCaseTeam on Location__c (after insert,after update) {
    List<ID> locList = new List<ID>();
    System.debug('trigger.new'+trigger.new);
    for(Location__c l:trigger.new){
        
        if(l.Status__c=='Completed'){
            
            System.debug('l.id'+l.id);
            locList.add(l.id);
        }
    }
    System.debug('locList'+locList);
    Map<id,List<CaseTeamMember>> caseMap= new Map<id,List<CaseTeamMember>>();
    List<Location__c> caseIds= new List<Location__c>();
    caseIds=[select Cases__r.id from Location__c where id in: locList];
    System.debug('caseIds'+caseIds);
    for(Location__c lc:caseIds){
        if(lc.Cases__r.id!=null)
            caseMap.put(lc.Cases__r.id,new List<CaseTeamMember>([select Member.Email from caseTeamMember where ParentId=:lc.Cases__r.id]));
        
        
    }
    System.debug('caseMap'+caseMap);
    List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
    
    if(caseMap.size()>0){
        Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
        
        List<String> sendTo = new List<String>();
        List<CaseTeamMember> ctmList= new List<CaseTeamMember>();
        for(Id l:caseMap.keySet())
        {
            System.debug('caseMap.get(l)'+caseMap.get(l));
            if(caseMap.get(l)!=null)
                system.debug('In if'+caseMap.get(l));
            ctmList.addAll((caseMap.get(l)));
        }
        System.debug('ctmList'+ctmList);
        for(CaseTeamMember ctm : ctmList){
            sendTo.add(ctm.Member.Email);
        }
        System.debug('sendTo'+sendTo);
        mail.setToAddresses(sendTo);
        mail.setSubject('Test Subject');
        String body = 'Test Content';
        mail.setHtmlBody(body);
        mails.add(mail);
    }
    System.debug('mails'+mails);
    if(mails.size()>0)    
        Messaging.sendEmail(mails);
}

All Answers

sowmya Inturi 9sowmya Inturi 9
Hi Aparna,

If the relationship between case and Location object is master detail, You can create a Roll Up summary field on Case which counts all the Location object records which has status 'Completed' . Based on the count (count>0), Create a process builder to send the email to Case Team Members.
--- Let me know if you need more clarification.

If the relationship id Lookup, You can create a trigger as below:

trigger EmailToCaseTeam on Location__c (after insert,after update) {
    List<ID> locList = new List<ID>();
    System.debug('trigger.new'+trigger.new);
    for(Location__c l:trigger.new){
        
        if(l.Status__c=='Completed'){
            
            System.debug('l.id'+l.id);
            locList.add(l.id);
        }
    }
    System.debug('locList'+locList);
    Map<id,List<CaseTeamMember>> caseMap= new Map<id,List<CaseTeamMember>>();
    List<Location__c> caseIds= new List<Location__c>();
    caseIds=[select Cases__r.id from Location__c where id in: locList];
    System.debug('caseIds'+caseIds);
    for(Location__c lc:caseIds){
        if(lc.Cases__r.id!=null)
            caseMap.put(lc.Cases__r.id,new List<CaseTeamMember>([select Member.Email from caseTeamMember where ParentId=:lc.Cases__r.id]));
        
        
    }
    System.debug('caseMap'+caseMap);
    List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
    
    if(caseMap.size()>0){
        Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
        
        List<String> sendTo = new List<String>();
        List<CaseTeamMember> ctmList= new List<CaseTeamMember>();
        for(Id l:caseMap.keySet())
        {
            System.debug('caseMap.get(l)'+caseMap.get(l));
            if(caseMap.get(l)!=null)
                system.debug('In if'+caseMap.get(l));
            ctmList.addAll((caseMap.get(l)));
        }
        System.debug('ctmList'+ctmList);
        for(CaseTeamMember ctm : ctmList){
            sendTo.add(ctm.Member.Email);
        }
        System.debug('sendTo'+sendTo);
        mail.setToAddresses(sendTo);
        mail.setSubject('Test Subject');
        String body = 'Test Content';
        mail.setHtmlBody(body);
        mails.add(mail);
    }
    System.debug('mails'+mails);
    if(mails.size()>0)    
        Messaging.sendEmail(mails);
}
This was selected as the best answer
aparna d 1aparna d 1
Thanks a lot sowmya its working .