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
Laura StanleyHubbardLaura StanleyHubbard 

Email Last Case Comment - Help with Code

I'm trying to create a visualforce email template that I can attach in a workflow that goes out to certain users/account teams whenever a case comment is created/modified.
User-added image

I can load the Apex Class but not the email template or component. I get the following error for component:

User-added image
Error with email bc  Ican't create component! Do I need to create a custom field for comments named last case comment?

User-added image
 
Swayam@SalesforceGuySwayam@SalesforceGuy
Hello,

You need to change the component name  as well as Its Value in componenet Refrence 

Please find below updated code :


Component :
Component :

<apex:component controller="SaaslastCaseComment" access="global">
<apex:attribute name="lastCommentCaseId" description="Salesforce Id of the Case" type="Id" assignTo="{!caseId}" />
<apex:outputtext value="{!lastCaseComment}" />
</apex:component>


Email Template :

<c:SaaslastCaseComment  lastCommentCaseId="{!relatedTo.Id}"/>


Please let me know, If it works don't forget to mark it as a best answer.

--
Regards,
Swayam
@Salesforceguy



 
Laura StanleyHubbardLaura StanleyHubbard
I pasted that as a visualforce in my email tempate and got an error of the following:

Error: <messaging:emailTemplate> is required and must be the outermost tag in the markup at line 1 column 1
Swayam@SalesforceGuySwayam@SalesforceGuy
Yes Use Your Code  Like below for Email Template

[User-added image]

Just Replace  this line  <c:SaaslastCaseComment caseId="{!relatedTo.Id}"/> with <c:SaaslastCaseComment lastCommentCaseId="{!relatedTo.Id}"/>

Let me know
Swayam@SalesforceGuySwayam@SalesforceGuy
User-added image
Laura StanleyHubbardLaura StanleyHubbard
it says that component doesnt exist, but it does here:
User-added image

Email template with error:

User-added image:

Apex Class Again

User-added image

So frustrating. Appreciate the help! This is certainly not my space!
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey,

Just Update You Email Template Code with the following , You are refrering wrong Component name,

C: ----- is the name of your visualforce page component 

<c:LastCommentComponent lastCommentCaseId="{!relatedTo.Id}" />


--
Regards,
Swayam 
​@salesforceguy
 
Laura StanleyHubbardLaura StanleyHubbard
All Code has been accepted, but my email template sends this errors (but still allowed me to save it):  List has no rows for assignment to SObject. Please try editing your markup to correct the problem. " I wonder if there's something wrong with where I'm saving/editing, etc. Can someone take a look. Included screen shots of location/field values within Setup.

Email Template:

<messaging:emailTemplate recipientType="Contact"
    relatedToType="Case"
    subject="{!relatedTo.Subject}">
    <messaging:htmlEmailBody >
        <html><body>
    Dear customer...
        Description: {!relatedTo.Description}
        Last comment: <c:LastCommentComponent lastCommentCaseId="{!relatedTo.Id}" />
        </body></html>
    </messaging:htmlEmailBody>
<messaging:plainTextEmailBody >
  ...
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

User-added image

Visualforce Component:
<apex:component controller="SaaSlastCaseComment" access="global">
<apex:attribute name="lastCommentCaseId" description="Salesforce Id of the Case" type="Id" assignTo="{!caseId}" />
<apex:outputtext value="{!lastCaseComment}" />
</apex:component>

User-added image
Apex Class:
public class SaaSlastCaseComment { 
public Id caseId {get; set;} 
public String lastCaseComment {  
get { 
            String lcc = [SELECT CommentBody FROM CaseComment WHERE ParentId = :caseId AND IsPublished = True ORDER BY LastModifiedDate DESC LIMIT 1].CommentBody; 
            return lcc; 
        } 
        set; 
    }  
}

User-added image
Paul PervinklerPaul Pervinkler
Like the error says, you've got the possibility of no rows being returned (i.e. there have been no comments on the case yet).  What you need to do is retrieve a list, grab the first entry, if present, or null otherwise.  Like this (note, I'm typing this on the fly in the comment box; I haven't tried running it yet):

public class SaaSlastCaseComment { 
public Id caseId {get; set;} 
public String lastCaseComment {  
get { 
            List<CaseComment> comments = [SELECT CommentBody FROM CaseComment WHERE ParentId = :caseId AND IsPublished = True ORDER BY LastModifiedDate DESC LIMIT 1];
            String lcc = comments.size()>0 ? comments[0].CommentBody : null;
            return lcc; 
        } 
        set; 
    }  
}
Pichala PalanikumarPichala Palanikumar
This is a life saver for me. Thanks Paul.