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
Connor CainConnor Cain 

Case Comment Test Class for Update

I can't get a test class to work for a Trigger that is only looking for when a Case Comment is Updated.

trigger validation on CaseComment (before update) {  
    if(Trigger.isUpdate){
        for(CaseComment caseCmt : Trigger.new){
            if(caseCmt.CreatedById != System.UserInfo.getUserId()){
                caseCmt.addError('You can not edit Someone else\'s Comment');
            }
        }
    }
}

Any Help/Advice would be appreciated:)
Best Answer chosen by Connor Cain
Raj VakatiRaj Vakati
Try this
 
@isTest
public class CaseCOmmentsTest{
 
    
    static testMethod  void createTestData(){
		Case   tCase = new Case();

        tCase.Status = 'Open';
        tCase.Description = 'Test Description';
        tCase.Origin = 'Annuity External';
        tCase.Type = 'Feature Request';
        tCase.Business_Impact__c = 'No Impact';
        tCase.Priority = 'Low';
      
        INSERT tCase;
        
     CaseComment   tComment = new CaseComment();
        tComment.ParentId = tCase.Id;
        tComment.CommentBody = 'Some Comment';
        tComment.IsPublished = TRUE;
        
        INSERT tComment;
        
      Profile  tProfile = 
            [
                SELECT Id 
                FROM   Profile 
                WHERE  Name = 'System Admin'
            ];
            
       User tUser = new User(
            Alias = 'standt', 
            Email='standarduser@teasdasdasdasdstorg.com', 
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = tProfile.Id, 
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='testuser@someteasdasdasdasdstorg.com'
        );
        
        INSERT tUser;       
		
		 System.runAs(tUser){
			 tComment.CommentBody = 'Some C123123123omment';
			 update tComment ;
			}
    }
   
}

 

All Answers

Chuck RamseyChuck Ramsey
Please post the portion of your test class that attempts to exercise Before Update. Hard to advise you when I don't know what you've tried.
Raj VakatiRaj Vakati
Try this
 
@isTest
public class CaseCOmmentsTest{
 
    
    static testMethod  void createTestData(){
		Case   tCase = new Case();

        tCase.Status = 'Open';
        tCase.Description = 'Test Description';
        tCase.Origin = 'Annuity External';
        tCase.Type = 'Feature Request';
        tCase.Business_Impact__c = 'No Impact';
        tCase.Priority = 'Low';
      
        INSERT tCase;
        
     CaseComment   tComment = new CaseComment();
        tComment.ParentId = tCase.Id;
        tComment.CommentBody = 'Some Comment';
        tComment.IsPublished = TRUE;
        
        INSERT tComment;
        
      Profile  tProfile = 
            [
                SELECT Id 
                FROM   Profile 
                WHERE  Name = 'System Admin'
            ];
            
       User tUser = new User(
            Alias = 'standt', 
            Email='standarduser@teasdasdasdasdstorg.com', 
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = tProfile.Id, 
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='testuser@someteasdasdasdasdstorg.com'
        );
        
        INSERT tUser;       
		
		 System.runAs(tUser){
			 tComment.CommentBody = 'Some C123123123omment';
			 update tComment ;
			}
    }
   
}

 
This was selected as the best answer
Connor CainConnor Cain
@isTest
public class ValidationTest {
    Static testMethod void TriggerTest(){
        CaseComment cc = new CaseComment();
        cc.CommentBody = 'cc test';
        update
    }
}

this is what i had so far, I also was working on creating a Case but lost the code. 

 
Chuck RamseyChuck Ramsey
What Raj did was to utilize the System.runAs method to simulate updating a CaseComment as a different User.  The update verb only works on existing records; you must read a record from the database before updating it. The test method for your validation would:
  1. Create a Case
  2. Create a CaseComment with its ParentId set to your Case Id.
  3. Create a test User. In order to prevent email messages being delivered where they shouldn't go, use @example.com as the email domain for any test User or Contact.
  4. Use System.runAs(testUser){} to create a code block to be executed as your test User.
  5. Attempt to update the CaseComment record you created earlier.
Let us know how you fare.
Raj VakatiRaj Vakati
Exactly what  Chuck Ramsey said .. 

Update the code as like below for safe side to handler the exception also 
@isTest
public class CaseCOmmentsTest{
 
    
    static testMethod  void createTestData(){
		Case   tCase = new Case();

        tCase.Status = 'Open';
        tCase.Description = 'Test Description';
        tCase.Origin = 'Annuity External';
        tCase.Type = 'Feature Request';
        tCase.Business_Impact__c = 'No Impact';
        tCase.Priority = 'Low';
      
        INSERT tCase;
        
     CaseComment   tComment = new CaseComment();
        tComment.ParentId = tCase.Id;
        tComment.CommentBody = 'Some Comment';
        tComment.IsPublished = TRUE;
        
        INSERT tComment;
        
      Profile  tProfile = 
            [
                SELECT Id 
                FROM   Profile 
                WHERE  Name = 'System Admin'
            ];
            
       User tUser = new User(
            Alias = 'standt', 
            Email='standarduser@teasdasdasdasdstorg.com', 
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = tProfile.Id, 
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='testuser@someteasdasdasdasdstorg.com'
        );
        
        INSERT tUser;       
		
		 System.runAs(tUser){
			 try{
			 tComment.CommentBody = 'Some C123123123omment';
			 update tComment ;
		 }catch(Exception e){
		 }
			}
    }
   
}


 
Mariah Carey 6Mariah Carey 6
Thank you very much! wordle (https://wordlewebsite.com)
Ali Raza 76Ali Raza 76
It really helped me, Thank you so much, So much Wordle (http://wordlewebsite.net)
mp3high ytmp3high yt
The code look great, I will take it for youtube MP3 Converter (https://mp3high.com) project, thanks!
Pham HoaiPham Hoai
I have used this snippet and successfully applied it to my https://wordle-unlimited.co/ website. It works very well.