Loading

Customize Salesforce Case Comment Related List with Inline Editing Using Visualforce and Apex Controller

Date de publication: Jun 23, 2026
Description

Since a standard Salesforce related list cannot be customized beyond the available configuration options, you need to create a custom Visualforce-based related list and embed it in the Page Layout to add custom functionality such as inline editing or custom actions. This article provides a working code example that creates a custom Visualforce-based related list for Case Comments, embedded in a Case page layout, that allows users to toggle the isPublished field value inline and save changes without a page redirect.

Résolution


The standard "save" functionality cannot be used since it redirects the page after the save. The solution below can be embedded into a Case Page Layout and allows users to modify the isPublished value of Case Comments inline.

Sample Code : Visual force page:
 

<!-- This is a Custom VisualForce page that will display the CaseComments related list. 
It also has input field for the user to change the value of isPublished on CaseComments. 
Embed this page into a Case Page Layout -->

<apex:page standardcontroller="case"  extensions="checkbox" tabstyle="case">
    <apex:pageBlock title="Case Comments" mode="new" >    
        <apex:form >
        <apex:commandButton value="Save" action="{!customSave}"/> 
            <!-- This is the Custom Save Button -->
                <apex:pageBlockTable value="{!Records}" var="index">    
                     <!-- The pageBlockTable iterates through the list of the custom Class -->
                    <apex:column > <apex:inputCheckbox value="{!index.published}"/> </apex:column>    <!-- Stores the input value from the user -->
                    <apex:column value="{!index.comment.isPublished}"/>    
                         <!--Display the CaseComments information -->
                    <apex:column value="{!index.comment.CommentBody}"/>
                </apex:pageblocktable>
        </apex:form>
    </apex:pageBlock>
</apex:page>


Sample Code : Apex Class
 

public class checkbox {
    public class AssignComment        
      // This class will be used to store the corresponding input from the user and the CaseComment.
    {  
        public CaseComment comment {get; set;}  //This will store the CaseComment.
        public Boolean published {get; set;} //This will store the input from the user.
        public AssignComment(){}   //Empty constructor.
    }    
    public List<assignComment> Records {get; set;}   
        //This will store the list of CaseComments as well as the corresponding user inputs.
    public checkbox(ApexPages.StandardController controller)
    {
        Records = new List<AssignComment>();          
        Case Record = (Case) controller.getRecord();   
          //Get case from controller.
        for (CaseComment Node : [Select commentBody, isPublished from CaseComment where parentId = :Record.Id])   
           //Query and loop through all the CaseComments.
        {
            assignComment temp = new AssignComment();  
            // Create temp to insert into the list Records.
            temp.comment = Node;
            temp.published = Node.isPublished;
            Records.add(Temp);
        }
    }
    public PageReference CustomSave()    
      //This class will take the input from the user and update the corresponding CaseComments.
    {
        List<CaseComment> updateList = new List<CaseComment>();    
        //Create a list of CaseComments to be updated.
        for (AssignComment a : Records)   //Loop through Records.
        {
            a.comment.isPublished = a.published;    
             //Update CaseComments with user input.
            updateList.add(a.comment);
        }
        update(updateList);
        return null;
    }  
}

 

Customization Notes

This code is a sample implementation for the Case Comment related list. Before deploying to production: replace the isPublished field references with your actual target field API names; replace the CaseComment object references with the target related object; add error handling for DML exceptions (try/catch with ApexPages.addMessage); consider adding CRUD/FLS checks before querying and updating; test with your org's specific page layout and profile assignments.

Numéro d’article de la base de connaissances

000384992

 
Chargement
Salesforce Help | Article