function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Carter85Carter85 

Help with query not populating a list.

I'm puzzled as to why a query I'm working on is not returning any results, particularly when I tested the same query in the DataLoader as first to check that it would pull the results I wanted.  My class and method are below:

public with sharing class CM_ContractStatusUpdate{

public CM_ContractStatusUpdate(){
    
     }
   
    public void updateContracts(){
     List<MG_Contract_Holder__c> statUpd = new List<MG_Contract_Holder__c>();
     statUpd = [SELECT ID FROM MG_Contract_Holder__c WHERE Contract_Status__c = 'Active' AND Expiration_Date__c < TODAY LIMIT 99];
        if(statUpd.size() > 0){
        for(Integer i = 0; i < statUpd.size(); i ++){
         MG_Contract_Holder__c upd = [SELECT Contract_Status__c FROM MG_Contract_Holder__c WHERE ID =:statUpd[i].ID];
         upd.Contract_Status__c = 'Canceled';
         update upd;
         }
        }
     }

}
However, each time I run it the statUpd list fails to populate, even though my tests with the DataLoader returned the results I was expecting.  Would appreciate any thoughts on the error.
Swati GSwati G
Hi,

Please try one by one following things and see if you are getting results:
1. Remove with sharing from class and execute the code.
2. Remove Expiration_Date__c < TODAY filter and execute code.

You will come to know the cause of this behaviour.


Carter85Carter85
Unfortunately in trying the suggestions listed it still does not generate results.
Carter85Carter85
Never mind, the problem actually lay with the test I was running to evaluate the class, I was distracted when setting it up and forgot to give it she seeAllData permission.
Swati GSwati G
ok.. so you were facing issue in test class. Its a best practise to create you own data instead of relying or org data.