Print this page

Customize Related Lists using VisualForce and Apex

Knowledge Article Number 000003207

Since a standard related list cannot be customized, you'll need to create a custom related list using Visualforce and embed it into the Page Layout. This can be done by querying and displaying the related records, then creating a custom action which takes the input from the User and updates the related records. Learn how you can accomplish these steps using the example below. 


The standard "save" functionality cannot be used since it redirects the page after the save. The sample code below can be embedded into a Case Page Layout and allows Users to modify the "isDisplayed" value of the Case Comments.


<!-- 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}"/>


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;
    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.
        return null;

promote demote