+ Start a Discussion
Meredith Salget 12Meredith Salget 12 

Receiving "INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]" error

Hello ~ I'm an admin that just started at a new company and cleaning up code of a developer that has left. There were a bunch of hard-coded Ids in code and configurations and I've attempted to replace them, in part, with a Custom Hierarchy Setting. I updated some code to reference the Id__c field on the custom setting instead of hard-coding the id, but I'm getting the "INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId]" error. The code was hard-coding a Queue Id (named "Trash") and setting the owner of a Case record. My custom setting is named Trash_Queue__c. I've ensured that the Custom Setting is populated with the correct 18 character Id for the Queue. When I use execute anonymous to test if I am setting the trashQueue variable correctly, I get the result I want. What am I missing? Here is a snippet of the code:
 
@isTest
private class TestSchedulableClass {
    
    static testmethod void test() {
        test.startTest();
        
        Trash_Queue__c trashQueueSetting = Trash_Queue__c.getOrgDefaults();
            
        String trashQueue = trashQueueSetting.Id__c;
        
        Case delOldCase= new Case();
        delOldCase.OwnerId =trashQueue;
        insert delOldCase;

        Test.stopTest();   
     }
}

Thanks!
Meredith
Alexander TsitsuraAlexander Tsitsura
Hi Meredith,

You code snippet is test. This TestMethod not view all database, you need initialize Trash_Queue__c settings.

i Recomended u to use @testSetup method
@testSetup static void setup() {
    Trash_Queue__c t = Trash_Queue__c .getOrgsDefaults();
    t.Id__c = [select Id from Group where Name = 'My Group' and Type = 'Queue'][0].Id;
    upsert t;
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
Meredith Salget 12Meredith Salget 12

It looks like what I actually need to do to access the data in the org is to use IsTest(SeeAllData=true).

Meredith Salget 12Meredith Salget 12
Now getting another error: 

No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation. External entry point

Guess I'm going to have to start from the beginning.
Ketankumar PatelKetankumar Patel
Hi Meredith, 

Its not a good practice to use IsTest(SeeAllData=true). When Developer sandbox doesn't have any data your test class would fail. Best Practice is you should always create test data in your test method.
Meredith Salget 12Meredith Salget 12
Hello @Ketankumar Patel~ I read that it's not a good idea to use IsTest(SeeAllData=true) in most cases. However, in this case, I am testing the data stored in a custom setting. Is there another way to do this? I am not a developer and just trying to update someone else's code to make it more scalable.

Thanks!
Meredith
David Holland 6David Holland 6
Meredith

All custom settings etc should be created at run time, for the reasons noted above.