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
devloper sfdcdevloper sfdc 

I need to write test class for casecomment trigger

trigger UpdLstMdfyDtFrmComt on CaseComment (before insert , before update) {

   Map<Id, Case> cases = new Map<Id, Case>();
    DateTime dT = System.now();
   Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
    for(CaseComment record:Trigger.new)
    {
         if (record.ParentId != null)
         {
               cases.put(record.ParentId, new Case(Id=record.ParentId, LastModifyDateCmtCas__c=myDate));
         }
    }
     update cases.values();

 
}
Best Answer chosen by devloper sfdc
Raj VakatiRaj Vakati
I got 100 % in my org with the below code
 
@isTest
private class UpdLstMdfyDtFrmComtTest {
    static testMethod void testCaseFromEmail() {
        
        System.Test.starttest();
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.runAs(uu)
        {
            
            
            
            
            Case tCase = new Case();
            
            tCase.Status = 'Open';
            tCase.Description = 'Test Description';
            tCase.Origin = 'Annuity External';
            tCase.Type = 'Feature Request';
            
            tCase.Priority = 'Low';
            
            insert tCase;
            
            CaseComment tComment = new CaseComment();
            tComment.ParentId = tCase.Id;
            tComment.CommentBody = 'Some Comment';
            tComment.IsPublished = TRUE;
            
            insert tComment;
            
            
            tComment.CommentBody = 'Inporgreess  Comment';
            update tComment ;   
            
        }
        System.Test.stoptest();
    }
}

 

All Answers

Raj VakatiRaj Vakati
try this 
 
@isTest
private class UpdLstMdfyDtFrmComtTest {
    static testMethod void testCaseFromEmail() {

        Test.starttest();
Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
		 System.runAs(uu)
        {
         

         

        Case tCase = new Case();

        tCase.Status = 'Open';
        tCase.Description = 'Test Description';
        tCase.Origin = 'Annuity External';
        tCase.Type = 'Feature Request';
         
        tCase.Priority = 'Low';
      
        insert tCase;
        
       CaseComment tComment = new CaseComment();
        tComment.ParentId = tCase.Id;
        tComment.CommentBody = 'Some Comment';
        tComment.IsPublished = TRUE;
        
        insert tComment;
        
        
 tComment.CommentBody = 'Inporgreess  Comment';
     update tComment ;   

    }
	Test.stoptest();
}
}

 
devloper sfdcdevloper sfdc
not working
Raj VakatiRaj Vakati
Can you check is there any error you are getting
 
Raj VakatiRaj Vakati
I got 100 % in my org with the below code
 
@isTest
private class UpdLstMdfyDtFrmComtTest {
    static testMethod void testCaseFromEmail() {
        
        System.Test.starttest();
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.runAs(uu)
        {
            
            
            
            
            Case tCase = new Case();
            
            tCase.Status = 'Open';
            tCase.Description = 'Test Description';
            tCase.Origin = 'Annuity External';
            tCase.Type = 'Feature Request';
            
            tCase.Priority = 'Low';
            
            insert tCase;
            
            CaseComment tComment = new CaseComment();
            tComment.ParentId = tCase.Id;
            tComment.CommentBody = 'Some Comment';
            tComment.IsPublished = TRUE;
            
            insert tComment;
            
            
            tComment.CommentBody = 'Inporgreess  Comment';
            update tComment ;   
            
        }
        System.Test.stoptest();
    }
}

 
This was selected as the best answer
devloper sfdcdevloper sfdc
Thanks Raj its working