3

I get the following error when I run the following class:

public with sharing class CaseUpdatePriorityEmailBodyMisc {
    public void updateCaseWhenDiscriptionFindBadWord(){
        List<Case> updateCasePriority = new List<Case>();
        for (Case badCase: (List<Case>)[FIND 'Scam OR Ponzi OR Lawyer OR Fraud OR Misrepresent OR Angry OR Upset OR Pissed OR Attorney' IN ALL FIELDS RETURNING Case(Id, CreatedDate, Priority)][0]){
            if (badCase.Priority != 'High'){
                badCase.Priority = 'High';
                updateCasePriority.add(badCase);
            }
        }
        update updateCasePriority;
    }
}

Test class:

@isTest
private class CaseUpdatePriorityEmailBodyMiscTest {

    public static Case setupCase(){
        Case newCase = (Case)SmartFactory.createSObject('Case');
        newCase.Status = 'Angry of with the service';
        newCase.Priority = 'Low';
        insert newCase;
        return newCase;
    }

    @isTest static void testBadWordInCaseEscalateCasePriority() {
        Case newCase = setupCase();
        String [] fixedSearchResults= new String[1];
        fixedSearchResults[0] = newCase.Status;
        Test.setFixedSearchResults(fixedSearchResults);
        Test.startTest();
            CaseUpdatePriorityEmailBodyMisc casePriorityUpdate = new CaseUpdatePriorityEmailBodyMisc();
            casePriorityUpdate.updateCaseWhenDiscriptionFindBadWord();
        Test.stopTest();
        System.assertEquals([Select Priority from Case where Id=:newCase.Id].Priority, 'High');
    }
}

Stack Trace:

Unable to Access Page
OrderedDict([(u'@xsi:nil', u'true')])

When I run the code using anonymous apex it works. Any explanation?

3

2 Answers 2

1

You're passing Test.setFixedSearchResults() a String array containing the Case Status, but it needs an array of record Ids. Change the two lines

String [] fixedSearchResults= new String[1];
fixedSearchResults[0] = newCase.Status;

to

Id [] fixedSearchResults= new Id[1];
fixedSearchResults[0] = newCase.Id;

and it works just fine.

1

Initially, I suspected the problem was related to the fact you've declared your class as public with sharing. That would have explain why you could run the code using anonymous Apex as an administrator, but couldn't get it to work without commenting out the Find query if running under different credentials that didn't have the right permissions to access it. I could see where a standard user might not have the proper access to to run the query. The only problem with that theory is that a test normally runs under system credentials and I don't see a runAs in your code to tell it to run as a user with a particular profile (something I'd recommend doing if you're using with sharing).

I'm assuming you've already prepared the data for the query to search from using Smart Factory? If not, you'll need to use the (seeAllData=true) modifier immediately following @isTest in your test class. That would definitely explain the error since the query wouldn't be able to access the data if it can't see the source of the data.

2
  • Yes I am creating data using SmartFactory. Did add (SeeAllData=true) but same result. Commented Jun 28, 2013 at 18:00
  • Does return newCase; from public static Case setupCase(){ return a list or only an individual case? Your class appears to me to be looking for a list (otherwise, why the for loop?), but your test class appears to only be creating a single case is why I ask. I'm not much of a SOSL user, so I'm not the one to try and debug your query. I also see that @DanielHoechst has pointed you to a SOSL reference.
    – crmprogdev
    Commented Jun 29, 2013 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .