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
KRITI LAHA 8KRITI LAHA 8 

Query on securityhealthcheck is not supported when using in APEX code

Hello all,
I was trying to write a batch job which will calculate score and risk types from SecurityHealthCheck,but I am getting below error :
Invalid type: Schema.SecurityHealthCheck
Invalid type: Schema.SecurityHealthCheckRisks

I am using below queries: 
select Score from SecurityHealthCheck
SELECT SettingRiskCategory, RiskType, Setting, SettingGroup, OrgValue, StandardValue FROM SecurityHealthCheckRisks where RiskType != 'MEETS_STANDARD' 
These two are running in Query editor.
Can anyone please give me solution how can I omit this error?
Thanks
VinayVinay (Salesforce Developers) 
Hi Kriti,

This error message indicates that Salesforce is unable to recognize the Schema.SecurityHealthCheck type in your code.

Below are check points:
  • You can check if SecurityHealthCheck feature is enabled in your org by going to Setup > Security Health Check.
  • Your Apex code is not authorized to access the SecurityHealthCheck object. To access this object, your code must have the necessary permissions. You can check if your code has the necessary permissions by going to Setup > Apex Test Execution and running a test that references the SecurityHealthCheck object.
  • There is a typo or other syntax error in your code. Double-check that you have spelled the SecurityHealthCheck object correctly and that you have imported the Schema namespace in your Apex class.
Please mark as Best Answer if above information was helpful.

Thanks,
KRITI LAHA 8KRITI LAHA 8
Hello Vinay,

Thanks for your reply. I have started writting code in this way but getting the same error..Can you please help me to write this batch class which will calculate score and risk categoy?

global class SecurityHeathCheckBatch implements Database.Batchable<sObject>, Database.Stateful {
    
    global String csvColumnHeader;
    global List<String> csvRowValues = new List<String>();
   
    global Database.QueryLocator start(Database.BatchableContext BC){
       
        String query ='SELECT Score FROM SecurityHealthCheck';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        
        for(SecurityHealthCheck healthcheck : (List<SecurityHealthCheck>) scope){
            
            String score = healthcheck.score!= null ? healthcheck.score : '';
            
        }
    }
   
    global void finish(Database.BatchableContext BC){
        
    }
}

Thanks