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
Brandon WermesBrandon Wermes 

FeedItem body text to update case status

First off, I'm extremely new to Apex (and programming in general). My company uses Service Cloud and Case Feed quite heavily.
One of the functions that we want to implement is the ability for a Case Feed user to enter "Case Closed" at the end of a Case Feed post and then have a Trigger automatically update the status of the case (marking the case "Complete").

Here is what I have so far:

trigger UpdateCaseCloseStatus on FeedItem (after insert) {
    List<Case> updates = new List<Case>();
    for (FeedItem fi : Trigger.new) {
        if (fi.ParentId.getSObjectType() == Case.SObjectType){
            }
            else if (fi.Body == 'Case closed'){
                updates.add(new Case(
                    Id = fi.ParentId,
                    Status = 'Complete'
                    ));
        }
    }
    update updates;
    }

The Trigger saves, however when entering "Case Closed" into a feed post, the case does not change to "Complete".

Any help is greatly appreciated!
Best Answer chosen by Brandon Wermes
Anupam RastogiAnupam Rastogi
Hi Brandon,

I have few things to mention for the code you have shared.

1. Firstly, you have taken an assumption that the User will enter only 'Case Closed' in the feed, which might not be the case always. So instead of using else if (fi.Body == 'Case closed'), try using if (fi.Body.endsWithIgnoreCase('Case Closed')).
2. I see that you are alltogether creating a new case when the feed contains Case Closed. This is not desired, right? You wish to update the existing case for which the feed has been entered with Status = Complete.
3. I assume that you already have added Status = Complete in the Case Status field picklist.

Here is the modified code. Please replace the existing code with this piece and test. And share you results.
 
trigger UpdateCaseCloseStatus on FeedItem (after insert) {
    
    List<Case> listOfCases = new List<Case>();
    
    for (FeedItem fi : Trigger.new) {
        
        //--- This retrieves the case for which feed has been entered.
        Case c = [select id from Case where Id = :fi.ParentId];

        //--- Now we check if the feed ends with text 'Case Closed'. We are checking case insensitive text.
        if (fi.Body.endsWithIgnoreCase('Case Closed')) {
            c.Status = 'Complete';
        }
        listOfCases.add(c);
    }
    update listOfCases;
}

Thanks
AR

If you find the reply useful that solves your problem then please mark it as best.

All Answers

Anupam RastogiAnupam Rastogi
Hi Brandon,

I have few things to mention for the code you have shared.

1. Firstly, you have taken an assumption that the User will enter only 'Case Closed' in the feed, which might not be the case always. So instead of using else if (fi.Body == 'Case closed'), try using if (fi.Body.endsWithIgnoreCase('Case Closed')).
2. I see that you are alltogether creating a new case when the feed contains Case Closed. This is not desired, right? You wish to update the existing case for which the feed has been entered with Status = Complete.
3. I assume that you already have added Status = Complete in the Case Status field picklist.

Here is the modified code. Please replace the existing code with this piece and test. And share you results.
 
trigger UpdateCaseCloseStatus on FeedItem (after insert) {
    
    List<Case> listOfCases = new List<Case>();
    
    for (FeedItem fi : Trigger.new) {
        
        //--- This retrieves the case for which feed has been entered.
        Case c = [select id from Case where Id = :fi.ParentId];

        //--- Now we check if the feed ends with text 'Case Closed'. We are checking case insensitive text.
        if (fi.Body.endsWithIgnoreCase('Case Closed')) {
            c.Status = 'Complete';
        }
        listOfCases.add(c);
    }
    update listOfCases;
}

Thanks
AR

If you find the reply useful that solves your problem then please mark it as best.
This was selected as the best answer
Brandon WermesBrandon Wermes
Thanks for the help AR.

I have the trigger code in and it is working. Now I'm trying to get the Test Class to pass, however am running into an error stating:

<span unselectable="on" "="" style="font-weight: normal; display: block; padding: 3px 4px; overflow: hidden; margin-left: 0px;">
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateCaseCloseStatus: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.UpdateCaseCloseStatus: line 8, column 1: []

My test class is:

@isTest
public class TestFeedPost    {
    static testMethod void insertNewFeedPost()     {
        
        FeedItem feedToCreate = new FeedItem();
        
        feedToCreate.ParentId    =       '500q0000001Pavd';
        feedToCreate.Body        =       'Blah blah blah close case';
        feedToCreate.Type        =        'TextPost';
        
        insert feedToCreate;
        }
        }

Thanks again for the help!
Anupam RastogiAnupam Rastogi
Hi Brandon,

What is line 8 in your trigger? Share the latest trigger code as well.

Also did you see the second point I mentioned in my earlier reply. You should not be creating a new case.

Thanks
AR