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
Nish321Nish321 

Issue with trigger to Insert CaseComment when a case is created

Hi,   It is throwing an error  "Error: Compile Error: Variable does not exist: com.ParentId at line 9 column 7".

Could someone please help me what's wrong here?

trigger CaseCommentsUpdate on Case (after insert, after update) {

List<CaseComment> NewComment = new List<CaseComment>();
    
    for(Case ca: Trigger.new){
    if(ca.Next_Contact_Date__c <= Today() && ca.status = 'Client Action Required' )
        
        CaseComment com = new CaseComment();
      com.ParentId = ca.id;
        com.CommentBody= ca.description;
        NewComment.add(com) ;                            

}}
Pankaj_GanwaniPankaj_Ganwani
Hi Ramshah,

You will have to wrap if condition code within curly braces.

for(Case ca: Trigger.new){
    if(ca.Next_Contact_Date__c <= Today() && ca.status = 'Client Action Required' ){
        
        CaseComment com = new CaseComment();
      com.ParentId = ca.id;
        com.CommentBody= ca.description;
        NewComment.add(com) ;                            

}}}
Vijay NagarathinamVijay Nagarathinam
Hi,

Use the below code it will work,
 
trigger CaseCommentsUpdate on Case (after insert, after update) {
List<CaseComment> NewComment = new List<CaseComment>();    
    for(Case ca: Trigger.new){
            if(ca.Next_Contact_Date__c <= Today() && ca.status = 'Client Action Required' ){
                  CaseComment com = new CaseComment();
                   com.ParentId = ca.id;
                   com.CommentBody= ca.description;
                   NewComment.add(com) ;    
             }                        
     }
     if(NewComment.size() > 0){
            insert NewComment;
     }
}

Thanks,
Vijay
Amit Chaudhary 8Amit Chaudhary 8
you forget to add proper opening and closing curly braces { } after if. Please try below code.
trigger CaseCommentsUpdate on Case (after insert, after update) {

List<CaseComment> NewComment = new List<CaseComment>();
    
    for(Case ca: Trigger.new)
	{
		if(ca.Next_Contact_Date__c <= Today() && ca.status = 'Client Action Required' )
        {
			CaseComment com = new CaseComment();
			com.ParentId = ca.id;
			com.CommentBody= ca.description;
			NewComment.add(com) ;
		}		
	}
	if(NewComment.size() > 0 )
	{
		insert NewComment;
	}
}
Let us know if this will help you
 
Nish321Nish321
Thanks all. Your feedback helped !!!