+ Start a Discussion
sumit dsumit d 

chatter feedItem is received by case team member via trigger

Hi All,
my need is that any time any chatter post is going on case Case Team Members should also receive it.
like whenever Any Chatter post is going on Case than the Case team member is also recieve that post?
my helper class is given below:-
public  without sharing class CaseFeedTriggerHelper {
    public static List<FeedItem> newCasesFeed = new List<FeedItem>();
    public static List<FeedItem> oldCasesFeed = new List<FeedItem>();
    public static Map<Id, FeedItem> newMapCasesFeed = new Map<Id, FeedItem>();
    public static Map<Id, FeedItem> oldMapCasesFeed = new Map<Id, FeedItem>(); 
    
    //Method to add case team members in case record followers.
    public static void AlertToCaseTeamMembers(){
        //set to retrive All cases from case feed
        Set<Id> caseIds = new Set<Id>();
        // link feedItem to Case
        for( FeedItem fi : newCasesFeed ){
            if( fi.ParentId != null && String.valueOf(fi.ParentId).startsWith('500') ){
                caseIds.add(fi.ParentId);
            }
        }
        
        //In case there was a feed insertion on an object other than case
        if (caseIds.size() > 0) { 
            //Map to retrive case team member and caseids
            Map<Id, Id> caseIdToCaseMemberId = new Map<Id, Id>();
            //Because there can be more than one team member
            List<EntitySubscription> listEntitySub = new List<EntitySubscription>();
             // add CaseTeamMembers to Map
            for( CaseTeamMember ctm : [ SELECT id, ParentId, 
                                       MemberId
                                       FROM CaseTeamMember 
                                       WHERE Parentid IN : caseIds
                                      ]){
                   caseIdToCaseMemberId.put(ctm.ParentId , ctm.MemberId);                        
            }
            //check if map has value or not
            if(caseIdToCaseMemberId.size() > 0){
                //create a entitySubscription for case  
                for ( Id caseIdsFromMap : caseIdToCaseMemberId.keySet() ) {
                    EntitySubscription entitySub = new EntitySubscription();
                    entitySub.ParentId = caseIdsFromMap;
                    entitySub.SubscriberId = caseIdToCaseMemberId.get(caseIdsFromMap);
                    listEntitySub.add(entitySub);
                }
            }
            //check if list of EntitySubscription has value
            if (listEntitySub.size() > 0) {
                insert listEntitySub;
            }
        }
    }
}
Any suggestion?
Best Answer chosen by sumit d
sowmya Inturi 9sowmya Inturi 9
Hi,
Please try the below code:
Case Team member will receive the chatter post and the mail will also be sent to the case Team Member.


trigger EmailOnCaseFeedToCaseTeamMember on FeedItem (after insert,after update) {
    
    Map<id,String> fiMap = new Map<Id,String>();
    Set<Id> cId = new Set<Id>();
    for(FeedItem f: trigger.new){
        System.debug('f'+f);
        if(f.ParentId.getSObjectType() == Case.SObjectType) {   
            cId.add(f.ParentId);
            fiMap.put(f.Id, f.Body);
        }
    }
    System.debug('fiMap'+fiMap);
    Case c= new Case();
    List<CaseTeamMember> ctmList= new List<CaseTeamMember>();
    
    List<FeedItem> fiList = new List<FeedItem>();
    
    System.debug('fiMap.keySet()'+fiMap.keySet());
    ctmList=[SELECT Id,Member.Email,Member.Name,ParentId FROM CaseTeamMember where ParentId in:fiMap.keySet()];
    System.debug('ctmList'+ctmList);
    for(CaseTeamMember ctm : ctmList){
        FeedItem post = new FeedItem();
        post.ParentId = ctm.MemberId;
        post.Body = fiMap.get(ctm.ParentId).replaceAll('\\<.*?\\>', '') +'test';
        System.debug('fiMap.get(ctm.ParentId)'+fiMap.get(ctm.ParentId));
        fiList.add(post);
        System.debug('post'+post);
        
        
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        System.debug('ctmList in Email'+ctmList);
        
        System.debug('ctm'+ctm);
        message.toAddresses = new String[] { ctm.Member.Email };
            System.debug('ctm.Member.Email'+ctm.Member.Email);
        message.optOutPolicy = 'FILTER';
        message.subject = 'New Case Feed';
        message.HTMLBody = '<b>Message Body:</b>' +fiMap.get(ctm.ParentId).replaceAll('\\<.*?\\>', '') +
            '<b>Created by:</b>' + ctm.Member.Name   ;
        System.debug('fiMap.get(ctm.ParentId)'+fiMap.get(ctm.ParentId));
        
        Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
        
        
    }
    insert fiList;
}


Thanks,
Sowmya.

All Answers

sumit dsumit d
i want that the feed shows to case team members via email . is it possible by this code?
sowmya Inturi 9sowmya Inturi 9
Hi,
Please try the below code:
Case Team member will receive the chatter post and the mail will also be sent to the case Team Member.


trigger EmailOnCaseFeedToCaseTeamMember on FeedItem (after insert,after update) {
    
    Map<id,String> fiMap = new Map<Id,String>();
    Set<Id> cId = new Set<Id>();
    for(FeedItem f: trigger.new){
        System.debug('f'+f);
        if(f.ParentId.getSObjectType() == Case.SObjectType) {   
            cId.add(f.ParentId);
            fiMap.put(f.Id, f.Body);
        }
    }
    System.debug('fiMap'+fiMap);
    Case c= new Case();
    List<CaseTeamMember> ctmList= new List<CaseTeamMember>();
    
    List<FeedItem> fiList = new List<FeedItem>();
    
    System.debug('fiMap.keySet()'+fiMap.keySet());
    ctmList=[SELECT Id,Member.Email,Member.Name,ParentId FROM CaseTeamMember where ParentId in:fiMap.keySet()];
    System.debug('ctmList'+ctmList);
    for(CaseTeamMember ctm : ctmList){
        FeedItem post = new FeedItem();
        post.ParentId = ctm.MemberId;
        post.Body = fiMap.get(ctm.ParentId).replaceAll('\\<.*?\\>', '') +'test';
        System.debug('fiMap.get(ctm.ParentId)'+fiMap.get(ctm.ParentId));
        fiList.add(post);
        System.debug('post'+post);
        
        
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        System.debug('ctmList in Email'+ctmList);
        
        System.debug('ctm'+ctm);
        message.toAddresses = new String[] { ctm.Member.Email };
            System.debug('ctm.Member.Email'+ctm.Member.Email);
        message.optOutPolicy = 'FILTER';
        message.subject = 'New Case Feed';
        message.HTMLBody = '<b>Message Body:</b>' +fiMap.get(ctm.ParentId).replaceAll('\\<.*?\\>', '') +
            '<b>Created by:</b>' + ctm.Member.Name   ;
        System.debug('fiMap.get(ctm.ParentId)'+fiMap.get(ctm.ParentId));
        
        Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
        
        
    }
    insert fiList;
}


Thanks,
Sowmya.
This was selected as the best answer
sowmya Inturi 9sowmya Inturi 9
Hi,
Please try the below code:
Case Team member will receive the chatter post and the mail will also be sent to the case Team Member.



trigger EmailOnCaseFeedToCaseTeamMember on FeedItem (after insert,after update) {
    
    Map<id,String> fiMap = new Map<Id,String>();
    Set<Id> cId = new Set<Id>();
    for(FeedItem f: trigger.new){
        System.debug('f'+f);
        if(f.ParentId.getSObjectType() == Case.SObjectType) {   
            cId.add(f.ParentId);
            fiMap.put(f.ParentId, f.Body);
        }
    }
    System.debug('fiMap'+fiMap);
    Case c= new Case();
    List<CaseTeamMember> ctmList= new List<CaseTeamMember>();
    
    List<FeedItem> fiList = new List<FeedItem>();
    
    System.debug('fiMap.keySet()'+fiMap.keySet());
    ctmList=[SELECT Id,Member.Email,Member.Name,ParentId FROM CaseTeamMember where ParentId in:fiMap.keySet()];
    System.debug('ctmList'+ctmList);
    for(CaseTeamMember ctm : ctmList){
        FeedItem post = new FeedItem();
        post.ParentId = ctm.MemberId;
        post.Body = fiMap.get(ctm.ParentId).replaceAll('\\<.*?\\>', '') +'test';
        System.debug('fiMap.get(ctm.ParentId)'+fiMap.get(ctm.ParentId));
        fiList.add(post);
        System.debug('post'+post);
        
        
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        System.debug('ctmList in Email'+ctmList);
        
        System.debug('ctm'+ctm);
        message.toAddresses = new String[] { ctm.Member.Email };
            System.debug('ctm.Member.Email'+ctm.Member.Email);
        message.optOutPolicy = 'FILTER';
        message.subject = 'New Case Feed';
        message.HTMLBody = '<b>Message Body:</b>' +fiMap.get(ctm.ParentId).replaceAll('\\<.*?\\>', '') +
            '<b>Created by:</b>' + ctm.Member.Name   ;
        System.debug('fiMap.get(ctm.ParentId)'+fiMap.get(ctm.ParentId));
        
        Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
        
        
    }
    insert fiList;
}

Thanks,
Sowmya.
sumit dsumit d
thank you sowmya it works
sumit dsumit d
i have a issue :- Case team member is not showing in follower List of case feed
Any suggestion?
sumit dsumit d
its solved Already ,i solved it thanks