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
VamsiVamsi 

can we create a new case comment for a case from apex class

Hi ,

I have a requirement where I need to display a vf page as pop -up  on button click on case and the vf page would include some text questions and once these are answered and user clicks on save button , then these text values should be inserted as a new comment to that particular case.

I have started working on this but when I create a new case comment by supplying required fields Parentid and commentbody. It throws me an error : Parentid doesn't exist or some time commentbody doesn't exists.

Can someone please help me....


Error: test Compile Error: Variable does not exist: CommentBody at line 13 column 8

public with sharing class test 
{
    public string text {get;set;}
    private CaseComment c ;
    
    public test ()
    {
        c = new CaseComment ();
    }
   public PageReference save() 
   {
       c = new CaseComment();
       c.CommentBody = text ;   ***** error *********
       c.ParentId = '5009000000dza7i';
       insert c ;
        
        return null;
   }

    
    
    
}
 
Best Answer chosen by Vamsi
Abhishek_DEOAbhishek_DEO

First thing, you should not be creating a class with name "test" as salesforce already has inbuild class with name "test", if you do so all your test classes may fail. on Same action, you are getting different erros sometimes for body and sometimes for parentid?

 

Could you please rename the class and try again?

All Answers

Abhishek_DEOAbhishek_DEO

First thing, you should not be creating a class with name "test" as salesforce already has inbuild class with name "test", if you do so all your test classes may fail. on Same action, you are getting different erros sometimes for body and sometimes for parentid?

 

Could you please rename the class and try again?

This was selected as the best answer
VamsiVamsi
After changing the class name it throws me the sames error variable doesn't exists CommentBody 
VamsiVamsi
Thank you Abhishek.

Issue resolved by changing the class name.