+ Start a Discussion
Brandon WermesBrandon Wermes 

System.QueryException: List has no rows for assignment to SObject when testing trigger with test class

Hello all,

I have a trigger that one of y'all helped me build (many thanks!). The trigger works perfectly to change the case status when a FeedItem is posted witha  Parent ID of a case where the feed body ends with "Case closed".
As noted, the trigger works fine in the Sandbox, however my Apex Class used for testing is failing due to the following error:
System.QueryException: List has no rows for assignment to SObject

The Apex Trigger is:
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;
}

The Apex 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;
        }
        }


The full error message is:
<span unselectable="on" "="" style="display: block; padding: 3px 4px; overflow: hidden; margin-left: 0px; color: rgb(34, 34, 34); font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; white-space: nowrap; background-color: rgb(218, 240, 249);">
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: []



Any assistance would be greatly appreciated.

Thanks!
Best Answer chosen by Brandon Wermes
BalajiRanganathanBalajiRanganathan
By default test methods does not have access to your org data.
You need to insert your test data in your test method or you need to use @isTest(SeeAllData=true) and get a case id using SOQL

All Answers

BalajiRanganathanBalajiRanganathan
By default test methods does not have access to your org data.
You need to insert your test data in your test method or you need to use @isTest(SeeAllData=true) and get a case id using SOQL
This was selected as the best answer
AMIT KAMBOJAMIT KAMBOJ
Please try this updated test class code. Please mark this as solution if this solves your question.
@isTest(SeeAllData=true) 
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;
        }
        }

 
Brandon WermesBrandon Wermes
Thank you both for the answers. Both were able to resolve the issue. I've tagged Balaji's as it was the first response.