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
Bundit Phetplay 15Bundit Phetplay 15 

How can I make a coverage for this trigger?

Hi Developers,

I'm from the functional side and this is my first time to write the trigger and test class

Here is the requirement that I got 'Once user tag me or manager in Chatter Feed --> Create Case with getting the information of where chatter feed has been posted'

I separated into 2 triggers

1.Create_CaseComment_From_Chatter

trigger Create_CaseComment_From_Chatter on FeedComment (before insert) { 

List<Chatter_Case_Reply__c> lstCase = new List<Chatter_Case_Reply__c>(); //Creating a list to save the comments section in the related list  
Set<Id> feedItemsId = new Set<Id>(); 

// Get ParentId from feeditem and create a map of feed items 
for (FeedComment f: Trigger.new) 
feedItemsId.add(f.feedItemId); 
Map<Id,FeedItem> fitems = new Map<Id,FeedItem>([Select id, Parentid from feedItem where id in :feedItemsId]); 


for (FeedComment f: Trigger.new) 
{ 
if(String.valueof(f.CommentBody) != '') 
{ 
Chatter_Case_Reply__c newCase = new Chatter_Case_Reply__c(); 
newCase.Comments__c = f.CommentBody; 

Id parId = fitems.get(f.feedItemId).ParentId; 
SObjectType sobType = parId.getSObjectType(); 
if (sobType == Case.SObjectType) { 
newCase.Case__c = parId; 
} 

lstCase.add(newCase); 
} 

} 
insert lstCase; 

}


2.ReplaceCharANDIsAssign <-- using for replacing some char to make it easier to read the case record

trigger ReplaceCharANDIsAssign on Case (before insert,before update) {
for(Case c : Trigger.new){
c.Description = c.Description.replaceAll('\\<.*?\\>', ' ');
c.Description = c.Description.replaceAll('&nbsp;', ' ');
c.Desc__c = c.Desc__c.replaceAll('\\<.*?\\>', ' ');
c.Desc__c = c.Desc__c.replaceAll('&nbsp;', ' ');
c.Description = c.Description.replaceAll('@Bundit Phetplay', '');
c.Description = c.Description.replaceAll('@Wuttisak Thabthimsaen', '');
c.Description = c.Description.replaceAll('@Thabthimsaen Wuttisak', '');
c.Description = c.Description.replaceAll('@Phetplay Bundit', '');
//c.Description = c.Description.replaceAll('#caseoth', ''); 
//c.Description = c.Description.replaceAll('#CASEOTH', ''); 
//c.Description = c.Description.replaceAll('#caseOTH', ''); 
//c.Description = c.Description.replaceAll('#CaseOth', '');
//c.Description = c.Description.replaceAll('#CaseOTH', '');  

if(String.valueof(c.Desc__c).Contains('Wuttisak')){
c.Is_assigned_to_Wuttisak__c = TRUE;
}

if(String.valueof(c.Desc__c).Contains('Bundit')){
c.Is_assigned_to_Bundit__c = TRUE;
}

}
}


My problem is I have no Idea how to write a test class for trigger '1.Create_CaseComment_From_Chatter' I only have 1 test class as the following

@isTest
private class testCreateCase {

static testMethod void mytestCreateCase() {

FeedItem post = new FeedItem();

post.ParentId = '0011000000oeLcH';
post.Body = '@Wuttisak Thabthimsaen @Bundit I need your help';

test.StartTest();
insert post;
test.StopTest();

    }
}

which coverage 100% on 2.ReplaceCharANDIsAssign trigger but 0% for the 1st trigger

If anyone has some advice or revise my test class would be appreciated!

Thank you

Best Answer chosen by Bundit Phetplay 15
Raj VakatiRaj Vakati
@isTest
private class testCreateCase {

static testMethod void mytestCreateCase() {
test.startTest();
Contact cont = new Contact();
            
            cont.Email ='abc@xyc.com';
            cont.lastname ='Business';
          
        insert cont;

        Case cs = new case ();
            cs.Status = 'test4';
            cs.CaseOrigin = 'Email';

// Other fields add here 
        insert cs;

        FeedItem post = new FeedItem();
        post.body = '[Welcome to test Knowledge]';
        Post.parentid = cs.Id;       

        insert post;


 
insert post;
test.StopTest();

    }
}