1

I have this select statement that fetches ID and a string from a custom object and creates a map using them. For some reason, when I run test, the select statement doesn't return any values. When I run the same set of statements in Execute Anonymous window, the results are returned just fine. Here's my code below:

// New Campuses
map<ID,String> mapCampusNew = new map<ID,String>();
List<Campus__c> campuses = [SELECT Id, Shortcode__c FROM Campus__c  WHERE Active__c = true];
for( Campus__c cm : campuses ) {
    mapCampusNew.put( cm.Id, cm.Shortcode__c );
}
System.debug( 'NCMap: ' + mapCampusNew );

Run within the trigger, this outputs:

23:02:45.684 (3684685025)|USER_DEBUG|[18]|DEBUG|NCMap: {}

while when run in Execute Anonymous window, I get:

Anonymous execution was successful.

29.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous:  // New Campuses
Execute Anonymous:  map<ID,String> mapCampusNew = new map<ID,String>();
Execute Anonymous:     List<Campus__c> campuses = [SELECT Id, Shortcode__c FROM Campus__c  WHERE Active__c = true];
Execute Anonymous:  for( Campus__c cm : campuses ) {
Execute Anonymous:      mapCampusNew.put( cm.Id, cm.Shortcode__c );
Execute Anonymous:  }
Execute Anonymous:  System.debug( 'NCMap: ' + mapCampusNew );
23:37:08.046 (46819693)|EXECUTION_STARTED
23:37:08.046 (46831803)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
23:37:08.047 (47769663)|SOQL_EXECUTE_BEGIN|[3]|Aggregations:0|select Id, Shortcode__c from Campus__c where Active__c = true
23:37:08.053 (53840428)|SOQL_EXECUTE_END|[3]|Rows:2
23:37:08.054 (54831491)|USER_DEBUG|[7]|DEBUG|NCMap: {a0f190000004CYcAAM=MIA, a0f190000004LAaAAM=SJC}
23:37:08.264 (54877967)|CUMULATIVE_LIMIT_USAGE
23:37:08.264|LIMIT_USAGE_FOR_NS|(default)|

How can I debug where it's going wrong?

Thank you.

  • Is the same user running the trigger and submitting the query in the anonymous apex window? – Richard N Aug 6 '14 at 16:45
  • Yes, Richard. Same user. – miCRoSCoPiC_eaRthLinG Aug 6 '14 at 16:57
  • When you run the code using test class you are not getting any result right? – tandonprateek Aug 6 '14 at 17:00
  • 4
    when you run in execute anonymous -- the org (sandbox) data is available for query; when you run in testmethod, unless you set up the test data as part of the testmethod (or use @isTest(SeeAllData=true) annotation on the test method), then no org data is available for the query – cropredy Aug 6 '14 at 17:02
  • 3
    Following on from @crop1645's point, while it is tempting to just add @isTest(SeeAllData=true), tests that rely on what is in the org tend to be fragile. So it is best if your tests do not use @isTest(SeeAllData=true) and instead insert specific data that they can then rely on in their asserts. – Keith C Aug 6 '14 at 17:08
1

When you run in execute anonymous -- the org (sandbox) data is available for query; when you run in a testmethod, unless you set up the test data as part of the testmethod (or use @isTest(SeeAllData=true) annotation on the test method), then no org data is available for the query.

However, I strongly agree with Keith C that seeAlldata=true should be avoided as it can lead to all sorts of problems when you deploy to PROD. Yes - it takes more time to set up testmethods but the setup time pays for itself for years to come.

With V31, one of the main reasons for using SeeAllData=true went away as you can now create custom pricebooks and pricebookEntries using a simple @isTest annotation.

In my systems, the only place I still need to use SeeAllData=true is when testing ContentVersion inserted into ContentWorkspace as the latter can't be created in a testmethod - a pretty limited use case

0

If the map is empty, that means that the SOQL query isn't returning any results.

As mentioned in the comments, it appears that this code is being executed in a test. Using SeeAllData=true is not best practice. In test execution, the test code can't see any other records so the code (thankfully) doesn't alter them! Best practice is to create whatever records you need in the test to run the test.

Example:

@isTest
public static void testCampusCode(){
    List<Campus__c> campusList = new List<Campus__c>();
    // add some active Campus records to the list
    for(Integer i = 0; i < 5; i++){
        campusList.add(new Campus__c(Name = 'Campus ' + String.valueOf(i),
                                     ShortCode__c = 'C' + String.valueOf(i),
                                     Active__c = true));
    }
    // add an inactive Campus
    campusList.add(new Campus__c(Name = 'Inactive Campus', 
                                 ShortCode__c = 'I1',
                                 Active = false));
    insert campusList;

    // now start the test
    Test.startTest();

    // your code
    Map<ID, String> mapCampusNew = new Map<ID, String>();
    List<Campus__c> campuses = [SELECT Id, Shortcode__c FROM Campus__c  WHERE Active__c = true];
    for( Campus__c cm : campuses ) {
        mapCampusNew.put( cm.Id, cm.Shortcode__c );
    }
    System.debug( 'NCMap: ' + mapCampusNew );

    // make sure you have only 5 active campuses in the map
    System.assert(mapCampusNew.size() == 5);

    // though we have 6 total Campuses (1 is inactive)
    System.assert(campusList.size() == 6);

    Test.stopTest();  
}

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.