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
Damien BellDamien Bell 

Unit test for apex trigger update datetime field on chatter comment

So I've created the following apex trigger, which updates a datetime field on the CASE object, which is updating the "last modified time" of the case in my sandbox -- here's the code for that.
 
trigger FeedThis on FeedComment(after insert, after update){

    List<Case> updates = new List<case>();
    List<id> userList = new List<ID>();
    List<Id> feedItemList = new List<id>();
    for(FeedComment fc: trigger.new){
        feedItemList.add(fc.FeedItemId);
        userList.add(fc.InsertedById);
    }
    Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,InsertedById,Visibility from feedItem where Id IN :feedItemList]);
    Map<Id, User> userMap = new Map<Id, User>([select id, usertype, name from user where ID IN :userList]);
    for(FeedComment fc: trigger.new){
        if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType) {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Chatter_Feed_Timestamp__c = System.now()
                    ));
        }
        
    }
    if(updates != null && updates.size() > 0)
    update updates;
}


I'm going to admit here that I am brand new to writing tests for SF -- and I'm sort of at a loss right now.  I'm trying to write a test for the above and didn't have much luck.   My thoughts are:

 

Given a new case is created, and a new chatter post is added, and a new comment is added to that chatter post, then the Last_Chatter_Feed_Timestamp__c > System.now() or something like that. 

Here's the code I came up with, which is horribly wrong:

 

@isTest
private class TestFeedThis {
    @isTest static void updateTimestampWithChatter(){
        //Create a new test case
        Case testCase = new Case(Status = 'New', Priority = 'Medium', Description = 'Test');
        
        //Create a new feed item (Post) on the test case
        Feeditem fi = new feeditem();
        fi.Body = 'test Post on case';
        fi.Type = 'TextPost';
        
        //Create a new comment on the post in the case
        fi.ParentId = testCase.Id;
        FeedComment fc = new FeedComment(CommentBody = 'test', FeedItemID = fi.Id);
        fc.CommentType = 'TextComment';
    }
    datetime now = System.now();
    System.assert(testCase.Last_Chatter_Feed_Timestamp__c > CreatedDate, 'true');
}
Could someone help me get this test working?

Thanks,
Raj VakatiRaj Vakati
Try like this .. you need to inset data in test class
 
@isTest
private class TestFeedThis {
    @isTest static void updateTimestampWithChatter(){
        //Create a new test case
        Case testCase = new Case(Status = 'New', Priority = 'Medium', Description = 'Test');
        insert testCase ; 
        //Create a new feed item (Post) on the test case
        Feeditem fi = new feeditem();
        fi.Body = 'test Post on case';
        fi.Type = 'TextPost';
        
        //Create a new comment on the post in the case
        fi.ParentId = testCase.Id;
		insert fi ; 
		
        FeedComment fc = new FeedComment(CommentBody = 'test', FeedItemID = fi.Id);
        fc.CommentType = 'TextComment';
		insert fc ; 
    }
  
}

 
Damien BellDamien Bell

I'm having some trouble with the case creation due to one of the items being a lookup.  I'm just trying to do

 

Account_Name__c = 'sdfsdf'

 

which is the account name in my sandbox environment -- but it's not working because it's a lookup -- can you help with that Raj?

Raj VakatiRaj Vakati
What is the data type of Account_Number__c ?? Ifs its fomrmual field you no need set and use this code
 
@isTest
private class TestFeedThis {
    @isTest static void updateTimestampWithChatter(){
		
		//create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
	
	
        //Create a new test case
        Case testCase = new Case(Status = 'New', Priority = 'Medium', Description = 'Test' , AccountId = acc.Id);
        insert testCase ; 
        //Create a new feed item (Post) on the test case
        Feeditem fi = new feeditem();
        fi.Body = 'test Post on case';
        fi.Type = 'TextPost';
        
        //Create a new comment on the post in the case
        fi.ParentId = testCase.Id;
		insert fi ; 
		
        FeedComment fc = new FeedComment(CommentBody = 'test', FeedItemID = fi.Id);
        fc.CommentType = 'TextComment';
		insert fc ; 
    }
  
}