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
Will LylesWill Lyles 

How to replace a CaseComment item with string and send to a table

I have the following code.  I would like to replace the CreatedBy.Name field with some text when it matches and automated account we have.

What would be the best way to replace this, send it to a list so I can then use that list in my pageBlockTable.
 
public my_Test_Apex(ApexPages.StandardController stdController)
{
       this.myCase = (Case)stdController.getRecord();
        
        //get case comments
        Case com = [select Id, CaseNumber,(select ParentId,CommentBody,CreatedDate,CreatedBy.Name from CaseComments ORDER BY CreatedDate DESC) from Case where Id =: ApexPages.CurrentPage().getParameters().get('Id') ];
        caseCommentList = new List<CaseComment>();
        for(CaseComment cs : com.CaseComments)
        {
           if(cs.CreatedBy.Name.contains('NameToReplace'))
           {
               //replace 'NameToReplace' with 'Support Representative'
           }
           else
           {
               caseCommentList.add(cs);
           }
            
        }       
}

Thanks.
Best Answer chosen by Will Lyles
Will LylesWill Lyles
Disregard.  I finally got it working.  I forgot to instantiate my list on page load.

Thanks Lokesh for getting me on the right path!
public class Will_Test_Apex 
{
    Public List<ccWrapper> ccList {get;set;}
    
    public Will_Test_Apex(ApexPages.StandardController stdController){
        cComments();
    }
    
    public List<ccWrapper> cComments()
    {
        ccList = new List<ccWrapper>();
        for(CaseComment c : [SELECT ParentId,CommentBody,CreatedDate,CreatedBy.Name from CaseComment
                      WHERE ParentId =:ApexPages.CurrentPage().getParameters().get('Id')
            		 ORDER BY CreatedDate DESC])
        {
            if(c.CreatedBy.Name == 'Name To Replace'){
            	ccList.add(new ccWrapper(c.CreatedDate,'Support Representative',c.CommentBody));
            }
            else
            {
                ccList.add(new ccWrapper(c.CreatedDate,c.CreatedBy.Name,c.CommentBody));
            }
        }
        return ccList;
    }
    
    public class ccWrapper
    {
        public string ccBody{get; set;}
        public Datetime ccDate{get; set;}
        public string ccName{get; set;}
        
        public ccWrapper(Datetime cDate, string cName, string cBody)
        {
            ccName = cName;
            ccDate = cDate;
            ccBody = cBody;
        }
    }   
}

 

All Answers

Lokeswara ReddyLokeswara Reddy
Hi William,

You can refine your code to get list of case comments something like this
for(CaseComment cs : select ParentId,CommentBody,CreatedDate,CreatedBy.Name from CaseComments where parentId =: ApexPages.CurrentPage().getParameters().get('Id')  ORDER BY CreatedDate DESC){
    if(cs.CreatedBy.Name.contains('NameToReplace')) {
        //replace 'NameToReplace' with 'Support Representative'
    }else{
        caseCommentList.add(cs);
    }
}
This list can be referenced in VF pageblocktable

Refer this example link
https://help.salesforce.com/articleView?id=000205631&type=1

Lokesh...
 
Will LylesWill Lyles
Hi Lokesh.  I don't think this will do what I need it to.  My original code works (you version does look more efficient though).  I'm looking to add some code to the if statement that if a value for CreatedBy.Name equals some text, I want to replace that with something else.  Basically we have a service account that I want to replace the name to something more customer friendly.  I need to add code to line 3 in your updated code above.
Lokeswara ReddyLokeswara Reddy
Hi William,
If get your issue correctly, you are trying to manipulate query data before displaying in VF page, if so, you need to use wrapper class. something like this.
List<csWrapper> csList = new List<csWrapper>();

for(CaseComment cs : select ParentId,CommentBody,CreatedDate,CreatedBy.Name from CaseComments where parentId =: ApexPages.CurrentPage().getParameters().get('Id')  ORDER BY CreatedDate DESC){
    if(cs.CreatedBy.Name.contains('NameToReplace')) {
        //replace 'NameToReplace' with 'Support Representative'
		csList.add(cs.ParentId, 'Support Representative');
    }else{
        csList.add(cs.ParentId, cs.CreatedBy.Name);
    }
}       

public class csWrapper{
	public string ParentId {get;set;}
	public string createByName {get;set;}

	public void csWrapper(string id, string name){
		ParentId = id;
		createByName = name;
	}
}
Refer csList in your VF page.
 
Will LylesWill Lyles
Hi Lokesh,
I've been trying to build a wrapper as you suggested, but can't seem to get my page to even return a case number in my table, much less any comment information.

Can you spot anything that I'm doing wrong?

***My VF page***
<apex:page standardController="Case" extensions="Will_Test_Apex" >
    <apex:sectionHeader title="My Case Comments"></apex:sectionHeader>
    <div>
        <h1>
            CaseNumber:{!case.casenumber}
        </h1>
    </div>
    <br />
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageMessages />
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!ccList}" var="com" id="commentsSection" >
                    <apex:column headerValue="Case Number" value="{!com.ccNumber}" />
                    <apex:column headerValue="Comment By" value="{!com.ccName}" />
                    <apex:column headerValue="Comments" value="{!com.ccBody}" />                     
                </apex:pageBlockTable> 
            </apex:pageBlockSection>    
        </apex:pageBlock>
    </apex:form>
</apex:page>

***My Apex Code***
public class Will_Test_Apex 
{
    Public List<ccWrapper> ccList {get;set;}
    
    public Will_Test_Apex(ApexPages.StandardController stdController){}
    
    public List<ccWrapper> cComments()
    {
        ccList = new List<ccWrapper>();
        for(Case c : [SELECT Id, CaseNumber,
                      (SELECT ParentId,CommentBody,CreatedDate,CreatedBy.Name from CaseComments ORDER BY CreatedDate DESC) 
                      FROM Case
                      WHERE Id =:ApexPages.CurrentPage().getParameters().get('Id')])
        {
            ccList.add(new ccWrapper(c.CaseNumber));
        }
        return ccList;
    }
    
    public class ccWrapper
    {
        public string ccBody{get; set;}
        public date ccDate{get; set;}
        public string ccName{get; set;}
        public string ccNumber{get; set;}
        
        
        public ccWrapper(string cNumber)
        {
            ccNumber = cNumber;
        }
        
        public ccWrapper(date cDate, string cName, string cBody)
        {
            ccName = cName;
            ccDate = cDate;
            ccBody = cBody;
        }
    }
    
}

​​​​​​​
Will LylesWill Lyles
Disregard.  I finally got it working.  I forgot to instantiate my list on page load.

Thanks Lokesh for getting me on the right path!
public class Will_Test_Apex 
{
    Public List<ccWrapper> ccList {get;set;}
    
    public Will_Test_Apex(ApexPages.StandardController stdController){
        cComments();
    }
    
    public List<ccWrapper> cComments()
    {
        ccList = new List<ccWrapper>();
        for(CaseComment c : [SELECT ParentId,CommentBody,CreatedDate,CreatedBy.Name from CaseComment
                      WHERE ParentId =:ApexPages.CurrentPage().getParameters().get('Id')
            		 ORDER BY CreatedDate DESC])
        {
            if(c.CreatedBy.Name == 'Name To Replace'){
            	ccList.add(new ccWrapper(c.CreatedDate,'Support Representative',c.CommentBody));
            }
            else
            {
                ccList.add(new ccWrapper(c.CreatedDate,c.CreatedBy.Name,c.CommentBody));
            }
        }
        return ccList;
    }
    
    public class ccWrapper
    {
        public string ccBody{get; set;}
        public Datetime ccDate{get; set;}
        public string ccName{get; set;}
        
        public ccWrapper(Datetime cDate, string cName, string cBody)
        {
            ccName = cName;
            ccDate = cDate;
            ccBody = cBody;
        }
    }   
}

 
This was selected as the best answer