+ Start a Discussion
KCrock88KCrock88 

Trigger Help - Update Case Status based on Feed Comment

Hello! Hoping someone can help - we recently migrated from Case Comments to Case Feed Layouts within our community. 
  • When the "Case Status" field is in "Awaiting Customer Feedback" - we previously had workflow rules to kick the Case Status back to "Working" when a customer posted a comment
  • With Case Feed Layout - we were able to use Flow and Process Builder to replicate this action for a New Case Feed Item
  • With Case Feed Layout - we are struggling to replicate this functionality for a new Case Post Comment (customer posts a Feed Item Comment rather than a new feed Item)
What would be the best way to accomplish this? Not an Apex Developer by trade, but hoping this can be done with a relatively simple trigger - any feedback would be appreciated!
User-added image

 
Julie ODonnell 6Julie ODonnell 6
I have this same issue. Did you ever figure it out? 
Gokula KrishnanGokula Krishnan
Hi KCrock88,

I think you can achieve by trigger.
Reference: https://salesforce.stackexchange.com/questions/30730/trigger-update-case-field-based-on-feeditem

Thanks,
Amit Khanduri.Amit Khanduri.
HI Julie ODonnell 6 & KCrock88,
This worked for me ... You can try this .. Let me know if you have any doubt.
Trigger:
trigger SimpleTrigger on FeedItem (after insert) {
List<Id> caseIds = new List<Id>();
List<Case> updateCase = new List<Case>();
    for (FeedItem newFeed : Trigger.new) {
       if (newFeed.ParentId.getSObjectType() == Case.SObjectType) {
   caseIds.add(newfeed.ParentId);
   Case myCase = [Select Id, AccountId, Status FROM Case WHERE Id IN :caseIds];
           updateCase.add(mycase); 
           for(Case c : updateCase){
if(c.Status != 'Working' && c.AccountId != Null){
c.Status = 'Working';
        }
    }
}
}
update updateCase;
}
Evan ArbeitmanEvan Arbeitman

hey @kcrock88,

Regarding your second bullet point: "With Case Feed Layout - we were able to use Flow and Process Builder to replicate this action for a New Case Feed Item". Did you create a process builder on the feed item object? Whereas you needed a trigger for a case post comment?

Julie ODonnell 6Julie ODonnell 6
I ended up doing this - needed to use a trigger on FeedComment. We use Open instead of Working but you get the idea.



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,
                    Status = 'Open'
                    ));
        }
        
    }
    if(updates != null && updates.size() > 0)
    update updates;
}
Evan ArbeitmanEvan Arbeitman
Thanks Julie - this helps. One thing I'm trying to understand is when to a FeedItem trigger v.s. a FeedComment trigger. It seems like you can grab the parent ID (i.e the case record) from either trigger. In the event a user replies to a case using a feed, I'd imagine both triggers would work?
Julie ODonnell 6Julie ODonnell 6
I also had trouble understanding that. I think that you have to use the FeedComment because that is what is actually being created. Its not a feed item - just a comment ON a feed item. 
Evan ArbeitmanEvan Arbeitman
Alright, I'll give the FeedComment a try. Thanks for the help!