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) 
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; 


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', '');  

c.Is_assigned_to_Wuttisak__c = TRUE;

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

private class testCreateCase {

static testMethod void mytestCreateCase() {

FeedItem post = new FeedItem();

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

insert post;


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


You're setting the ParentId to an account. You instead should assign it a Case Id. Also, please note that you should read 000041398, particularly:

Create the necessary data in test classes, so the tests do not have to rely on data in a particular organization.

Create all test data before calling the starttest method.

You'll save yourself a lot of trouble if you avoid using hard-coded ID values in your unit tests. Instead, always create "dummy" records in your unit tests. They will not persist beyond the end of the test, as all changes to the database from the test will be discarded when the test runs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.