0

I'm having issues returning values in a Custom setting (Type = List) when I'm instantiating a batch class, and I'm not sure why.

I'm doing an upsert in my test class and confirming via system debug that the values are inserted.

        //Create Custom Setting
        RoundRobinAssignments__c RoundRobin = new RoundRobinAssignments__c(
            Name = 'Master', OpportunityNonPackageLeads__c = 5, 
            OpportunityPackageLead__c =10);
        upsert RoundRobin;  
        
        List <RoundRobinAssignments__c> thresholdTEST = [SELECT id,OpportunityPackageLead__c,
                      OpportunityNonPackageLeads__c  
                   FROM RoundRobinAssignments__c];
        System.debug('thresholdTEST: '+ thresholdTEST);

In my batch class, I've written the below (I've tried the select statement that has been commented out too)

        //List <RoundRobinAssignments__c> threshold = [SELECT id,OpportunityPackageLead__c,OpportunityNonPackageLeads__c  FROM RoundRobinAssignments__c];
        RoundRobinAssignments__c threshold = RoundRobinAssignments__c.getValues('Master');
        System.debug('threshold: '+ threshold); 

Debug Log: Debug Log

Thoughts?


update from comments:

Result: 23:08:14:286 USER_DEBUG [81]|DEBUG|allData: {} 

Calling Batch Class:

//execute the batch 
Test.startTest(); 
Database.executeBatch(new BatchOpportunityOwnerAssignment()); 
Test.stopTest();
4
  • what type is this custom setting? (Hierarchy or List)
    – Mark Pond
    Commented Sep 10, 2020 at 19:08
  • Type = List, Visibility = Public Commented Sep 10, 2020 at 20:26
  • Can you try outputting all data and see if the map is empty? Map<String, RoundRobinAssignments__c> allData = RoundRobinAssignments__c.getAll(); system.debug('allData: ' + allData); and can you post more of the unit test class to show how the batch class is being called?
    – Mark Pond
    Commented Sep 10, 2020 at 21:43
  • @MarkPond Result: 23:08:14:286 USER_DEBUG [81]|DEBUG|allData: {} Calling Batch Class: //execute the batch Test.startTest(); Database.executeBatch(new BatchOpportunityOwnerAssignment()); Test.stopTest(); Commented Sep 11, 2020 at 3:27

1 Answer 1

0

I'm not at a machine where I can confirm it but I suspect that the reason you are seeing this behavior is that your batch running within System.startTest() is in its own scope and may not see the values which you inserted earlier in your test method.

Here's an example cobbled together from the code provided above:

@isTest
private class BatchTest {

    static testMethod void testBatchDidGoodStuff() {

        // switch context to the test scope
        System.startTest();
    
        //Create Custom Setting
        RoundRobinAssignments__c RoundRobin = new RoundRobinAssignments__c(
            Name = 'Master', OpportunityNonPackageLeads__c = 5, 
            OpportunityPackageLead__c = 10);
        upsert RoundRobin;
    
        Database.executeBatch(new BatchOpportunityOwnerAssignment()); 
    
        System.stopTest(); // cause the batch to execute
    
        // System.assert(your logic); // assert the correct behavior occurred in the batch
    }
}

You must log in to answer this question.

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