I have written the following Apex trigger:
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 FROM User WHERE Support_Team_Member__c = true]);
for(FeedComment fc: trigger.new){
if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType) {
updates.add(new Case(
Last_Chatter_Feed_Timestamp__c = System.now()
));
Case c = [SELECT ID, Case_Assigned_To_Support__c, Support_First_response_minutes__c FROM CASE WHERE ID =: fc.ParentId];
if(userMap.containsKey(fc.InsertedById) && c.Support_First_response_minutes__c == null && c.Case_Assigned_To_Support__c != null && c.id != null){
long totalMs = DateTime.now().getTime() - c.Case_Assigned_To_Support__c.getTime();
totalMs *= 60000;
updates.add(new Case(
id = c.id,
Support_First_response_minutes__c = totalMs
));
}
}
}
update updates;
}
My test is just a hacky catchall (or in this case catch 78%)
@isTest
private class TestFeedThis {
@isTest static void updateTimestampWithChatter(){
//Create a new test case
Account a = new Account(name='test acc',phone='7777777777');
insert a;
Contact con = new Contact(accountid=a.id,email='test@test.com');
con.FirstName = 'Testy';
con.LastName = 'mcTest';
insert con;
Case testCase = new Case(Status = 'New', Priority = 'Medium', Description = 'Test', Last_Updated_By_Support__c = System.now(), Contact = con );
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 ;
}
}
When I execute the test I'm getting the following error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FeedThis: execution of AfterInsert
caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Trigger.FeedThis: line 28, column 1: []
I've tried to fix this for the past 3 hours any help would be greatly appreciated.