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
Satish Kumar LalamSatish Kumar Lalam 

Custom email alerts are not working for Customer Community users

I have Created a custom email alert notification with below code. we send email alerts when ever a comment is made on feeditem of case. My code is working fine for all users except Customer Community users. It is throwing below error when the user try to comment on feed from Customer portel. Kindly advice on how to resolve this issue.

FeedItemJiraIntegration: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, null: [] Class.emailAlertsOnCaseFeedItem.fireExternalEmail: line 69, column 1 Class.emailAlertsOnCaseFeedItem.fireEmailalerts: line 17, column 1 Trigger.FeedItemJiraIntegration: line 5, column 1

TRIGGER :
trigger FeedItemJiraIntegration on FeedItem (After insert) {
    
    if(trigger.isinsert && trigger.isafter){
        FeedItemTiggerHelper.afterInsert(trigger.newMap.keyset());
        emailAlertsOnCaseFeedItem.fireEmailalerts(trigger.newMap.keyset());
         
    }
   
 
}


Helper : 
public without sharing class emailAlertsOnCaseFeedItem {
    //@InvocableMethod(label='emailAlertsOnCaseFeedItem')
    //public static void fireEmailalerts(List<Id> feedItemIds){
    public static void fireEmailalerts(Set<Id> feedItemIds){
        Set<Id> caseIds = new Set<Id>();
        
        List<FeedItem> feedList = [select Id, IsRichText, Body, Visibility,  parentId from FeedItem where id =:feedItemIds ];
        
        Map<Id,FeedItem> caseFeedmap = new Map<Id,FeedItem>();
        system.debug('feedList = '+feedList.size());
        for(FeedItem fd1: feedList){
            caseFeedmap.put(fd1.ParentId,[select Id, IsRichText, Body, Visibility,  parentId from FeedItem where id =:fd1.Id]);
            caseIds.add(fd1.ParentId);
            system.debug('caseIds = '+caseIds);
            if(fd1.Visibility == 'AllUsers'){
                system.debug('fd1.Visibility = '+fd1.Visibility);
                fireExternalEmail(fd1.ParentId, fd1.Id);
                
                fireInternalEmail(fd1.ParentId, fd1.Id);
            }else{
                system.debug('fd1.Visibility = '+fd1.Visibility);
                fireInternalEmail(fd1.ParentId, fd1.Id);
                
            }
        }
        
    }
    Public static Void fireExternalEmail(Id caseId, Id feedId){
        Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
        FeedItem feedObj = [select Id, IsRichText, Body, Visibility,  parentId from FeedItem where id =:feedId];
        OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
        string caseUrl = 'https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id;
        List<Id> conIds = new List<Id>();
        List<string> toEmails = new List<string>();
        List<string> bccEmails = new List<string>();
        List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
        system.debug('External conList = '+conList);
        for (Contact con: conList){
            conIds.add(con.Id);
            bccEmails.add(con.Email);
            system.debug('External con ID = '+conIds);
        }
        toEmails.add(parentCase.ContactEmail); 
        system.debug('toEmails1 = '+toEmails);
        Messaging.reserveSingleEmailCapacity(2);
        Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
        
        
        mymail.setToAddresses(toEmails);
        
        mymail.setBccAddresses(bccEmails);

        mymail.setWhatId(parentCase.Id);
        
        mymail.setOrgWideEmailAddressId(owea.get(0).Id);
        mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
        if(feedObj.IsRichText == false){
            mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
          system.debug('PlainText');
        }
        else{
            mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
          system.debug('RichText');
        }
        
       
           System.debug('Statement after external insert.');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });

        
    }
    Public static Void fireInternalEmail(Id caseId, Id feedId){
        Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
        FeedItem feedObj = [select Id, IsRichText, Body, Visibility,  parentId from FeedItem where id =:feedId];
        OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
        
        List<Id> conIds = new List<Id>();
        List<Id> teamIds = new List<Id>(); 
        List<string> toEmails = new List<string>();
        List<string> bccEmails = new List<string>();
        List<Group> lstPublicGroup = [SELECT Id FROM Group WHERE Name= 'Support Team' and Type = 'Regular' LIMIT 1];
        if(lstPublicGroup != null && !lstPublicGroup.isEmpty()){
            for(GroupMember objMember : [SELECT GroupId, UserOrGroupId FROM GroupMember WHERE GroupId = :lstPublicGroup[0].Id]){
                if(String.valueOf(objMember.UserOrGroupId).startsWith('005')){
                    System.debug('User Id : ' +objMember.UserOrGroupId);
                    teamIds.add(objMember.UserOrGroupId);
                    
                }
            }
        }
        List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
        system.debug('Internal conList = '+conList);
        for (Contact con: conList){
            conIds.add(con.Id);
            bccEmails.add(con.Email);
            system.debug('External con ID = '+conIds);
        }
        toEmails.add(parentCase.ContactEmail);
        system.debug('toEmails2 = '+toEmails);
        
        Messaging.reserveSingleEmailCapacity(2);
        Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
        
        
        mymail.setToAddresses(teamIds);

        mymail.setWhatId(parentCase.Id);
       
        mymail.setOrgWideEmailAddressId(owea.get(0).Id);
        mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
        if(feedObj.IsRichText == false){
            mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
        }
        else{
            mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
        }
        
        
       
            System.debug('Statement after insert.');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });

        
    }
    
}
SwethaSwetha (Salesforce Developers) 
HI Satish,
Do you see any GACK Id(https://developer.salesforce.com/blogs/engineering/2015/02/gack) included with the UNKNOWN_EXCEPTION, null: message?


Also see related: https://salesforce.stackexchange.com/questions/94424/cant-add-contentpost-type-feeditems-on-cases-from-customer-community-user

Thanks
Satish Kumar LalamSatish Kumar Lalam
Hi Swetha,

Thank you for your response. I Could see that the Customer community licensed users cannot send emails from the below article. 

https://help.salesforce.com/s/articleView?id=sf.users_license_types_communities.htm&type=5

I have tried the future method aswell they didnt work. Kindly let me know if you any idea that we can send custom emails for Customer community users.