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
[Lindsey Kiken][Lindsey Kiken] 

Receiving Apex Trigger Compile Error: unexpected token: '<'

I have a trigger that successully fires upon the system receiving an inbound email related to a Case record. This trigger's purpose is to stamp the inbound email's body into a text field on the Case object. The working code is as follows:
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c = emailBodyMap.get(c.Id);
        
        update c;
        }
}
My users just asked for a new requirement in that the values stamped into the text field "[FromAddres] + BR() + [FromName] + BR() + [Subject] + BR() + BR() + [Body]". Sounded simple enough, but I ran into a silly issue: 

I received the error "Error: Compile Error: unexpected token: '<' at line 34 column 67". I have tried to play around with different ways of adding in <BR/>'s into this field, but continue to run into this same error code. Please see below for the updated work in progress code:
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
    Map<Id, String> emailSubjectMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailSubjectMap.put(so.ParentId, so.Subject);
            } 
        }
    Map<Id, String> emailFromAddressMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromAddressMap.put(so.ParentId, so.FromAddress);
            } 
        }       
    Map<Id, String> emailFromNameMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromNameMap.put(so.ParentId, so.FromName);
            } 
        }  
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c = emailFromNameMap.get(c.Id) + <br /> + emailFromAddressMap.get(c.Id) + <br /> + <br /> + emailSubjectMap.get(c.Id) + <br /> + <br /> + emailBodyMap.get(c.Id);
        
        update c;
        }
}
Thoughts?  
Best Answer chosen by [Lindsey Kiken]
Temoc MunozTemoc Munoz
Hi Lindsey,

Is  Latest_Email_Reply__c  a text field? If so, this is not possible.
If this is a text area you can simply use '\n'; http://salesforce.stackexchange.com/questions/74215/save-newline-in-text-field

Thanks

All Answers

Mahesh DMahesh D
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
    Map<Id, String> emailSubjectMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailSubjectMap.put(so.ParentId, so.Subject);
            } 
        }
    Map<Id, String> emailFromAddressMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromAddressMap.put(so.ParentId, so.FromAddress);
            } 
        }       
    Map<Id, String> emailFromNameMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromNameMap.put(so.ParentId, so.FromName);
            } 
        }  
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c = emailFromNameMap.get(c.Id) +' <br />' + emailFromAddressMap.get(c.Id) + '<br />  <br />' + emailSubjectMap.get(c.Id) +' <br /> <br /> '+ emailBodyMap.get(c.Id);
        
        update c;
        }
}

You can try this.

Please do let me know if it helps you.

Regards,
Mahesh
[Lindsey Kiken][Lindsey Kiken]
Mahesh,

I tried that route earlier, but it unfortunately then applies the <br> as the literal text vs the function. Any other thoughts?

Cheers,
Lindsey
Mahesh DMahesh D
Hi Lindsey,

Please make sure that Latest_Email_Reply__c is a text area / rich text area.

Regards,
Mahesh
Temoc MunozTemoc Munoz
Hi Lindsey,

Is  Latest_Email_Reply__c  a text field? If so, this is not possible.
If this is a text area you can simply use '\n'; http://salesforce.stackexchange.com/questions/74215/save-newline-in-text-field

Thanks
This was selected as the best answer
[Lindsey Kiken][Lindsey Kiken]
I knew that I was missing something unfortunately simple! In this particular situation, we do not want to introduce rich text and wanted to only capture the literal text. Final working code:
 
trigger CopyEmailReplyBody on EmailMessage (after insert) {
    Set<ID> caseSet = new Set<ID>();
    Map<Id, String> emailBodyMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailBodyMap.put(so.ParentId, so.TextBody);
            } 
        }
    Map<Id, String> emailSubjectMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailSubjectMap.put(so.ParentId, so.Subject);
            } 
        }
    Map<Id, String> emailFromAddressMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromAddressMap.put(so.ParentId, so.FromAddress);
            } 
        }       
    Map<Id, String> emailFromNameMap = new Map<Id, String>{};
        for (EmailMessage so : Trigger.new) {
            if(so.ParentId.getSObjectType() == Case.sObjectType && so.Incoming){
                caseSet.add(so.parentid);
                emailFromNameMap.put(so.ParentId, so.FromName);
            } 
        }  
        Map<Id,case> caseMAP = new Map<Id,case>([SELECT Latest_Email_Reply__c FROM Case WHERE id in:caseSet]);  
        for(Case c:caseMAP.values()){
        
            c.Latest_Email_Reply__c =   'Email Received From: ' + emailFromNameMap.get(c.Id) + ' (' + emailFromAddressMap.get(c.Id) + ')' + '\n' + 
                                        'Subject: ' + emailSubjectMap.get(c.Id) + '\n' + 
                                        'Body:' + '\n' + 
                                        emailBodyMap.get(c.Id);
        
        update c;
        }
}

Thank you again for all of your help!
Temoc MunozTemoc Munoz
Awesome. I'm glad it's working now!