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();
  • what type is this custom setting? (Hierarchy or List) – Mark Pond Sep 10 at 19:08
  • Type = List, Visibility = Public – Cory O. Bennett Sep 10 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 Sep 10 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(); – Cory O. Bennett Sep 11 at 3:27
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
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.