You need to sign in to do that
Don't have an account?
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(' ', ' '); c.Desc__c = c.Desc__c.replaceAll('\\<.*?\\>', ' '); c.Desc__c = c.Desc__c.replaceAll(' ', ' '); 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