I have a small problem that I'm hoping someone can help me with.
I have a simple VisualForce page and controller to list and insert case comments. It displays comments fine but when I try to insert comments their CommentBody is always left blank. I use a middleware variable because the user in question does not have FLS access to the field. However I've tested it as a full system administrator and had the same issue (blank comments).
VF Page:
<apex:page showHeader="true" sidebar="true" controller="ViewCaseController">
Lipsum<br/><br/>
<apex:repeat value="{!comments}" var="comment">
{!comment.CommentBody}<br/><br/>
</apex:repeat>
<apex:form>
<apex:inputField value="{!cmtext}" label="Comment" />
<apex:commandButton title="Add comment" value="Add comment" action="{!addComment}"/>
</apex:form>
</apex:page>
Controller:
public class ViewCaseController {
public Case c {get;set;}
public List<CaseComment> comments {get;set;}
public CaseComment cm {get;set;}
public String cmtext {get;set;}
public ViewCaseController() {
c = [SELECT Id FROM Case WHERE Id=:ApexPages.currentPage().getParameters().get('id')];
comments = [SELECT Id,CommentBody FROM CaseComment WHERE ParentId=:c.Id];
cm = new CaseComment(ParentId=c.Id);
cm.IsPublished = true;
}
public PageReference addComment() {
cm.CommentBody = cmtext;
insert cm;
return new PageReference('/ViewCase?id='+c.Id);
}
}
The page is accessed with the ID parameter set to the ID of a case.
Attribution to: Venko
Possible Suggestion/Solution #1
There are a few bugs in your code (as per my comments on your question) which means you will not be getting the behaviour you are expecting. Try this VF page and controller and it should work for you..
public class ViewCaseController {
public Case c {get;set;}
public List<CaseComment> comments {get;set;}
public CaseComment cm {get;set;}
public String cmtext {get;set;}
public ViewCaseController() {
c = [SELECT Id FROM Case WHERE Id=:ApexPages.currentPage().getParameters().get('id')];
comments = [SELECT Id,CommentBody FROM CaseComment WHERE ParentId=:c.Id];
cm = new CaseComment(ParentId=c.Id);
cm.IsPublished = true;
}
public PageReference addComment() {
cm.CommentBody = cmtext;
insert cm;
PageReference pr = new PageReference('/apex/ViewCase?id='+c.Id);
pr.setRedirect(true);
return pr;
}
}
and
<apex:page showHeader="true" sidebar="true" controller="ViewCaseController">
Lipsum<br/><br/>
<apex:repeat value="{!comments}" var="comment">
{!comment.CommentBody}<br/><br/>
</apex:repeat>
<apex:form>
<apex:inputText value="{!cmtext}" label="Comment" />
<apex:commandButton title="Add comment" value="Add comment" action="{!addComment}"/>
</apex:form>
</apex:page>
I have fixed the redirect in your controller and the input widget on your VF page.
The resulting page for me works (and reloads smoothly) for me:
Attribution to: Simon Lawrence
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31882