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
mars1 Rovermars1 Rover 

Database.query is not returning data

I am new in salesforce, I have a problem with Database.query. Its not returning the data to apex class, But while i am using query editor its showing the correct data. My query is
List<CM_Group__c> e = Database.query('SELECT 
        CM_Skill_Level__c,CM_Start_Date__c,Name FROM CM_Employee__c where 
        CM_Start_Date__c != null');
        System.debug(e);
when iam checking with debug log it showing empty. But when i am using query editor it showing the below result 
User-added image
Any help would be appreciatable
 
mars1 Rovermars1 Rover
@RAJ. its showing Method does not exist or incorrect signature: void query(List<CM_Employee__c>) from the type Database
mars1 Rovermars1 Rover
global class EMP_EmployeeAudit {
    

    global void execute(){
     List<CM_Employee__c> e = Database.query('SELECT CM_Skill_Level__c,CM_Start_Date__c,Name FROM CM_Employee__c where CM_Start_Date__c != null');
        System.debug('Entered in execute');
        System.debug(e);
        for(CM_Employee__c emp : scope){
             System.debug(emp.CM_Start_Date__c);
            if(emp.CM_Start_Date__c != null){
                integer diff = emp.CM_Start_Date__c.daysBetween(Date.today());
                integer totalYear = diff/365;
                
                if(totalYear < 1){
                    emp.CM_Skill_Level__c = 'Junior';
                }else if(totalYear >= 1 && totalYear <3){
                    emp.CM_Skill_Level__c = 'Semi Senior';
                }else if(totalYear >= 3 && totalYear <6){
                    emp.CM_Skill_Level__c = 'Senior';
                }else if(totalYear >= 6 && totalYear <10 ){
                    emp.CM_Skill_Level__c = 'Expert';
                }
                totalRecordUpdated++;
                e.add(emp);
            }
            
        }
        if(e != null && e.size()>0){
          update e;    
        }
        
    }    
    
}
Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi Mars1 Rover,

Please try the Query by Changing your Sobject List type.

List<CM_Employee__c> e = Database.query('SELECT CM_Skill_Level__c,CM_Start_Date__c,Name FROM CM_Employee__c where CM_Start_Date__c != NULL');
System.debug(e);

Hope it'll help you!

Thanks
DineshKumar
Raj VakatiRaj Vakati
Its Correct only .. He is referring the CM_Employee__c in batch code 

 
global void execute(){
     List<CM_Employee__c> e = Database.query('SELECT CM_Skill_Level__c,CM_Start_Date__c,Name FROM CM_Employee__c where CM_Start_Date__c != null');
        System.debug('Entered in execute');
        System.debug(e);
        for(CM_Employee__c emp : scope){
             System.debug(emp.CM_Start_Date__c);
            if(emp.CM_Start_Date__c != null){
                integer diff = emp.CM_Start_Date__c.daysBetween(Date.today());
                integer totalYear = diff/365;
                
                if(totalYear < 1){
                    emp.CM_Skill_Level__c = 'Junior';
                }else if(totalYear >= 1 && totalYear <3){
                    emp.CM_Skill_Level__c = 'Semi Senior';
                }else if(totalYear >= 3 && totalYear <6){
                    emp.CM_Skill_Level__c = 'Senior';
                }else if(totalYear >= 6 && totalYear <10 ){
                    emp.CM_Skill_Level__c = 'Expert';
                }
                totalRecordUpdated++;
                e.add(emp);
            }
            
        }
        if(e != null && e.size()>0){
          update e;    
        }
        
    }    
    
}