+ Start a Discussion
Shaun B.Shaun B. 

Case Feed vs. Feed Comment

Hello (Developer) World!

I wrote (with the help of the community!) the following Apex code to update a case field anytime someone posts to the case feed.  The issue I have now is that if someone replies to the feed comment instead of the feed item, my fields don't update.  Can someone help me to modify or add to the below to achieve my desired result?
 
trigger UpdateCaseCommentFields on FeedItem (after insert, after update) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility == 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_Internal_Comment__c = fi.Body
                    ));
        }
        else if (fi.ParentId.getSObjectType() == Case.SObjectType && fi.Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fi.ParentId,
                    Last_External_Comment__c = fi.Body
                    ));
        }
    }
    update updates;
}
Best Answer chosen by Shaun B.
GauravGargGauravGarg
Hi Bailey,

My bad, please replace below line at line 7:

List<id> userList = new List<ID>();

Please try now, let me know if you still face issues on this.

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com

All Answers

GauravGargGauravGarg
Hi Bailey,

Please add one more trigger on FeedComment, below is the snippet:
 
trigger updateFeedItemFields on FeedComment(after insert, after update){
	List<Case> updates = new List<Case>();
	List<Case> updates = new List<case>();
	list<Id> feedItemList = new List<id>();
	for(FeedComment fc: trigger.new){
		feedItemList.add(fc.FeedItemId);
	}
	Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,Visibility from feedItem where Id IN :feedItemList]);
	for(FeedComment fc: trigger.new){
		if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && feedMap.get(fc.feedItemId).Visibility == 'InternalUsers') {
			updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
		else if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && feedMap.get(fc.feedItemId).Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
    }
    if(updates != null && updates.size() > 0)
    update updates;
}


Please try adding above trigger, and let me know if you face any issues. 

Thanks

Gaurav
Email: gauravgarg.nmims@gmail.com

Shaun B.Shaun B.
Awesome!  I fixed two items below using your code.  Thanks for that!!

One more question, I am looking at the feedItemId.Visibility right now to set my fields, but how would I instead look at the User.UserType?  I want to filter based on the following:
  • if : User.UserType = "CspLitePortal"
  • else if : User.UserType = "Standard"
trigger UpdateFeedItemFields on FeedComment(after insert, after update){
    List<Case> updates = new List<case>();
    list<Id> feedItemList = new List<id>();
    for(FeedComment fc: trigger.new){
        feedItemList.add(fc.FeedItemId);
    }
    Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,Visibility from feedItem where Id IN :feedItemList]);
    for(FeedComment fc: trigger.new){
        if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && feedMap.get(fc.feedItemId).Visibility == 'InternalUsers') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
        else if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && feedMap.get(fc.feedItemId).Visibility != 'InternalUsers') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_External_Comment__c = fc.CommentBody
                    ));
        }
    }
    if(updates != null && updates.size() > 0)
    update updates;
}

 
GauravGargGauravGarg
Hi Bailey,

Please follow below steps:
1. List out all users (owners), just I did for FeedItem. 
2. Query on user with respective field i.e.UserType and store it in map. 
3. Update if condition for updating case record like:
if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.ownerId).userType == 'CPSLite') {


Hope this will solve your problem.

Regards,
Gaurav
Email: gauravgarg.nmims@gmail.com

Shaun B.Shaun B.
Sorry for my ignorance.  I am an Apex newbie!  I tried the following, but I'm getting an error stating: "Error: Compile Error: Invalid field ownerId for SObject FeedComment at line 11 column 134"
trigger UpdateFeedItemFields on FeedComment(after insert, after update){
    List<Case> updates = new List<case>();
    List<User> users = new List<user>();
    List<Id> feedItemList = new List<id>();
    for(FeedComment fc: trigger.new){
        feedItemList.add(fc.FeedItemId);
    }
    Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,Visibility from feedItem where Id IN :feedItemList]);
    Map<Id, FeedItem> userMap = new Map<id, FeedItem>([select id,Visibility from feedItem where Id IN :feedItemList]);
    for(FeedComment fc: trigger.new){
        if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.ownerId).userType != 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
        else if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.ownerId).userType == 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_External_Comment__c = fc.CommentBody
                    ));
        }
    }
    if(updates != null && updates.size() > 0)
    update updates;
}

 
GauravGargGauravGarg
Hi Bailey, 

Please update code as below:
trigger UpdateFeedItemFields on FeedComment(after insert, after update){
    List<Case> updates = new List<case>();
    List<User> userList = new List<user>();
    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 && userMap.get(fc.InsertedById).userType != 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
        else if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.InsertedById).userType == 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_External_Comment__c = fc.CommentBody
                    ));
        }
    }
    if(updates != null && updates.size() > 0)
    update updates;
}


Try above code and let me know if you face some issues.

Thanks,

Gaurav
Email: gauravgarg.nmims@gmail.com

Shaun B.Shaun B.
Thanks again for your help Gaurav.  The error I am seeing now is:
 
Compile Error: Incompatible element type Id for collection of User at line 7 column 9

It appears to have an issue with:
 
userList.add(fc.InsertedById);

Thanks!
GauravGargGauravGarg
Hi Bailey,

My bad, please replace below line at line 7:

List<id> userList = new List<ID>();

Please try now, let me know if you still face issues on this.

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
This was selected as the best answer
Shaun B.Shaun B.
Awesome!  That worked!!  Here is the final code:
 
trigger UpdateFeedItemFields 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 && userMap.get(fc.InsertedById).userType != 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_Internal_Comment__c = fc.CommentBody
                    ));
        }
        else if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType && userMap.get(fc.InsertedById).userType == 'CPSLitePortal') {
            updates.add(new Case(
                    Id = fc.ParentId,
                    Last_External_Comment__c = fc.CommentBody
                    ));
        }
    }
    if(updates != null && updates.size() > 0)
    update updates;
}

Now if only I had a test class...  :)
GauravGargGauravGarg

Hi Bailey,

Do you need the test class for above code?

Thanks,

Gaurav

Shaun B.Shaun B.
Nope, thank you.  I was able to get it working using the below code:
 
@isTest
public class UpdateFeedItemFieldsTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        
            Case caseObj = new Case (Status = 'Open', Priority = 'Low', Origin = 'Email', Test_Ticket__c = 'True');
            insert caseObj;
            
            FeedItem feed =  new FeedItem ();
            feed.ParentId = caseObj.Id;
            feed.Body = 'Hello Internal';
            feed.Visibility = 'InternalUsers';
            insert feed;
        
            feed =  new FeedItem ();
            feed.ParentId = caseObj.Id;
            feed.Body = 'Hello defi';
            feed.Visibility = 'InternalUsers';
            insert feed;
            
            FeedComment fc = new FeedComment();
            fc.CommentBody = 'test';
            fc.FeedItemId = feed.Id;
            insert fc;
            
        Test.StopTest();
        
    }
}

Thanks again for all your help!
GauravGargGauravGarg

No Problem, you can contact me directly through email or skype. Below are my details 

Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990
 

Thank you

Julie ODonnell 6Julie ODonnell 6
You guys rock ! I totally nabbed this and tweaked it for my situation. Thanks