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
Sem IngrafSem Ingraf 

EmailMessage modify TextBody and HtmlBody

Hi,
i have a trigger on the emailMessage object. I have a removeReplyText method that searches for some strings inside the TextBody and Htmlbody of the emailMessage object and deletes/hides it in order to streamline the display of the email when it is copied/stored on a case
 

private static void removeReplyText(List<EmailMessage> newObjects){
        Map<Id, Case> casesMap;
        Set<Id> caseIds = new Set<Id>();
        List<EmailMessage> emsToUpdate = new List<EmailMessage>();
        for(EmailMessage em : newObjects){
            System.debug('Clean body: ' + em.Id + ' - Parent: ' + em.ParentId);
            System.debug('Clean TXT: ' + em.TextBody);
            System.debug('Clean HTML: ' + em.HtmlBody);
            

            if(em.ParentId != null){
                caseIds.add(em.ParentId);
            }
        }
        casesMap = new Map<Id, Case>(getCases(caseIds));
        System.debug('CaseMap' +casesMap);
        
        

        for(EmailMessage em: newObjects){
            Case myCase = casesMap.get(em.ParentId);
            Boolean changed = false;
            if(myCase != null){
                System.debug('Found case');
                String body = em.TextBody;
                if(String.isNotBlank(em.TextBody)) {
                    Integer occurrence1 = body.indexOfIgnoreCase(Constants.EMAIL_QUOTEDBODY_1);
                    Integer occurrence2 = body.indexOfIgnoreCase(Constants.EMAIL_QUOTEDBODY_2);
                    Integer occurrence = occurrence1 != -1 && occurrence2 != -1 && occurrence1 < occurrence2 ? occurrence1 : occurrence2;

                    //system.debug('occurrence3' +occurrence3);

                    if (occurrence != -1) {
                        body = body.substring(0, occurrence);
//                        em.TextBody = body;
                        changed = true;
                    }
                    
        
                    system.debug('Constants.EMAIL_FooterPlain' +Constants.EMAIL_FooterPlain);

                    Integer occurrenceFooter = body.indexOfIgnoreCase(Constants.EMAIL_FooterPlain);
                    if (occurrenceFooter != -1) {
                        body = body.substring(0, occurrenceFooter);
//                        em.TextBody = body;
                        changed = true;
                    }
                }

                String htmlBody = em.HtmlBody;
                if(String.isNotBlank(em.HtmlBody)) {
                    System.debug('HTML Body');
                    Integer occurrenceh1 = htmlBody.indexOfIgnoreCase(Constants.EMAIL_QUOTEDBODY_1);
                    System.debug('occurrenceh1: ' + occurrenceh1);

                    Integer occurrenceh2 = htmlBody.indexOfIgnoreCase(Constants.EMAIL_QUOTEDBODY_2);
                    System.debug('occurrenceh2: ' + occurrenceh2);
                    

                    Integer occurrenceh = occurrenceh1 != -1 && occurrenceh2 != -1 && occurrenceh1 < occurrenceh2 ? occurrenceh1 : occurrenceh2;
                    System.debug('occurrenceh: ' + occurrenceh);
                    
                        

                    if (occurrenceh != -1) {
                        htmlBody = htmlBody.substring(0, occurrenceh);            
                        htmlBody += '</body> </html>';
//                        em.HtmlBody = htmlBody;
                        System.debug('new em.HtmlBody: ' + em.HtmlBody);
                        changed = true;
                    }
                    
                
                    system.debug('Constants.EMAIL_FooterHTML' +Constants.EMAIL_FooterHTML);

                    Integer occurrenceFooter = htmlBody.indexOfIgnoreCase(Constants.EMAIL_FooterHTML);
                    if (occurrenceFooter != -1) {
                        htmlBody = htmlBody.substring(0, occurrenceFooter);
//                        em.TextBody = body;
                        changed = true;
                    }
                }
                if (changed){
                    emsToUpdate.add(new EmailMessage(Id=em.Id, TextBody=body, HtmlBody=htmlBody));
                }

            }
        }

        update emsToUpdate;
    }
now I would like to modify this method to be able to delete/hide this initial piece as well :
Rif. Preventivo {{{Quote.Name}}} ({{{Quote.IdPreventivo__c}}})
Stato: {{{Quote.TranslatedStatus__c}}}
Mittente: {{{Sender.Name}}}



how could I do? is it possible to do this? I initially did a test with replace and replaceAll but without success.

Thanks for any help
Best Answer chosen by Sem Ingraf
Prateek Prasoon 25Prateek Prasoon 25
Yes, it is possible to modify the removeReplyText method to also remove the initial piece you mentioned. One approach could be to use regular expressions to match and remove the specific pattern you are looking for.
Here is an example of how you could modify the method to remove the initial pattern:
private static void removeReplyText(List<EmailMessage> newObjects){
    // existing code to get casesMap and set caseIds omitted for brevity
    
    // Define regular expression pattern to match the initial pattern you want to remove
    String pattern = 'Rif\\. Preventivo \\{\\{\\{Quote\\.Name\\}\\}\\} \\(\\{\\{\\{Quote\\.IdPreventivo__c\\}\\}\\}\\)\\s*Stato: \\{\\{\\{Quote\\.TranslatedStatus__c\\}\\}\\}\\s*Mittente: \\{\\{\\{Sender\\.Name\\}\\}\\}\\s*';
    for(EmailMessage em: newObjects){
        Case myCase = casesMap.get(em.ParentId);
        Boolean changed = false;
        if(myCase != null){
            String body = em.TextBody;
            if(String.isNotBlank(em.TextBody)) {
                // Use regular expression to replace the initial pattern with an empty string
                body = body.replaceAll(pattern, '');
                
                // existing code to remove quoted text and email footer omitted for brevity
                if (!body.equals(em.TextBody)) {
                    emsToUpdate.add(new EmailMessage(Id=em.Id, TextBody=body));
                    changed = true;
                }
            }
            String htmlBody = em.HtmlBody;
            if(String.isNotBlank(em.HtmlBody)) {
                // Use regular expression to replace the initial pattern with an empty string
                htmlBody = htmlBody.replaceAll(pattern, '');
                
                // existing code to remove quoted text and email footer omitted for brevity
                
                if (!htmlBody.equals(em.HtmlBody)) {
                    emsToUpdate.add(new EmailMessage(Id=em.Id, HtmlBody=htmlBody));
                    changed = true;
                }
            }
            if (changed) {
                // existing code to update email messages omitted for brevity
            }
        }
    }
}

If you find my answer helpful, please mark it as the best answer. Thanks!