Monday, January 30, 2012

Case Comment Related List

Component VF Code:


<apex:component controller="CaseCommentsComponentController" allowDML="true">
    <!-- Attribute Definition -->
    <apex:attribute name="CaseId" description="Salesforce Id of the Case whose Case Comments needs to be rendered" type="Id" required="true" assignTo="{!caseId}" />
    
    <!-- Component Body -->
    <apex:componentBody >
        <apex:form >
            <apex:pageBlock title="Case Comments" >
                <apex:pageBlockButtons location="top">
                    <apex:commandButton action="{!NewComment}" value="New"/>
                </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!Comments}" var="comment"> 
                    <apex:column headerValue="Action"> 
                        <apex:outputLink value="/{!comment.cComment.Id}/e?parent_id={!caseId}&retURL=/{!caseId}">Edit</apex:outputLink>&nbsp;&nbsp;
                        <apex:commandLink action="{!deleteComment}" value="Del">
                            <apex:param name="CommentId_d" value="{!comment.cComment.Id}"/>
                        </apex:commandLink>&nbsp;&nbsp; 
                        <apex:commandLink action="{!makePublicPrivate}" value="{!comment.PublicPrivateAction}">
                            <apex:param name="CommentId_p" value="{!comment.cComment.Id}" />
                        </apex:commandLink>
                    </apex:column>
                    <apex:column headerValue="Public" value="{!comment.cComment.IsPublished}" />
                    <apex:column headerValue="Comments">
                        <apex:outputText escape="false" value="{!comment.commentText}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>   
        </apex:form>    
    </apex:componentBody>
</apex:component>


Controller Code:


public with sharing class CaseCommentsComponentController {
    
    public Id caseId {get; set;}
    public cComments[] comments{
        get{
            List<cComments> comments = new List<cComments>();
            for(CaseComment comment : [Select LastModifiedDate, LastModifiedBy.Id, LastModifiedBy.Name, IsPublished, CreatedDate, CreatedBy.Id, CreatedBy.Name, CommentBody From CaseComment c where ParentId = :caseId order by c.LastModifiedDate desc])
            {
                cComments tempcComment = new cComments();
                tempcComment.cComment = comment;
                
                // Build String to display.
                tempcComment.commentText = '<b>Created By: <a href=\'/' + comment.CreatedBy.Id + '\'>' + comment.CreatedBy.Name + '</a> (' + comment.CreatedDate.format() + ') | ';
                tempcComment.commentText += 'Last Modified By: <a href=\'/' + comment.LastModifiedBy.Id + '\'>' +  comment.LastModifiedBy.Name + '</a> (' + comment.LastModifiedDate.format() + ')</b><br>';
                tempcComment.commentText += comment.CommentBody;
                
                if(comment.IsPublished)
                    tempcComment.PublicPrivateAction = 'Make Private';
                else
                    tempcComment.PublicPrivateAction = 'Make Public';
                //Add to list
                comments.add(tempcComment); 
            }
            return comments;
        }
        
        set;
    }
        
    public PageReference NewComment()
    {
        PageReference pr = new PageReference('/apex/new_comment?id='+caseId);
        pr.setRedirect(true);
        return pr;
    }
    
    public PageReference deleteComment()
    {
        Id commentId = ApexPages.currentPage().getParameters().get('CommentId_d');
        
        for(cComments Comment : comments)
        {
            if(Comment.cComment.Id == commentId)
            {   
                delete Comment.cComment;                
                break;
            }
        }
        
        PageReference pg = new PageReference('/' + caseId);
        pg.setRedirect(true);
        return pg;
    }   
    
    public PageReference makePublicPrivate()
    {
        Id commentId = ApexPages.currentPage().getParameters().get('CommentId_p');
        for(cComments Comment : comments)
        {
            if(Comment.cComment.Id == commentId)
            {   
                Comment.cComment.IsPublished = !Comment.cComment.IsPublished;
                if(Comment.cComment.IsPublished)
                    Comment.PublicPrivateAction = 'Make Private';
                else
                    Comment.PublicPrivateAction = 'Make Public';
                    
                update Comment.cComment;                
                break;
            }
        }
        PageReference pg = new PageReference('/' + caseId);
        pg.setRedirect(true);
        return pg;
    }
    
    public class cComments {
    
        public CaseComment cComment {get; set;}
        public String commentText {get; set;}
        public String PublicPrivateAction {get; set;}
    }
}

No comments:

Post a Comment