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
Raksha NarayanRaksha Narayan 

Question regarding Batch class

I want to query all fields in the start method of the batch class. Below is my code which is giving error(also giving error on type casting). Can you please suggest if there is any way to achieve this?
global class BatchClass implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) { 
    Map<String, Schema.SObjectField> CaseMap = Case.sObjectType.getDescribe().fields.getMap();        
    Set<String> setFieldNames = CaseMap.keySet();
    list<String> lstFieldNames = new List<String>(setFieldNames);                
    String query = Database.query('SELECT ' + String.join(lstFieldNames, ',') + ' FROM Case limit 2');
    return Database.getQueryLocator(query);
    }
}
 
Best Answer chosen by Raksha Narayan
Dosbol TDosbol T
Hi there, please try this one. 
global class BatchClass implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) { 
        Map<String, Schema.SObjectField> CaseMap = Case.sObjectType.getDescribe().fields.getMap();        
        Set<String> setFieldNames = CaseMap.keySet();
        list<String> lstFieldNames = new List<String>(setFieldNames);                
        String query = 'SELECT ' + String.join(lstFieldNames, ',') + ' FROM Case LIMIT 2';
        return Database.getQueryLocator(query);
    }
}

 

All Answers

Dosbol TDosbol T
Hi there, please try this one. 
global class BatchClass implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) { 
        Map<String, Schema.SObjectField> CaseMap = Case.sObjectType.getDescribe().fields.getMap();        
        Set<String> setFieldNames = CaseMap.keySet();
        list<String> lstFieldNames = new List<String>(setFieldNames);                
        String query = 'SELECT ' + String.join(lstFieldNames, ',') + ' FROM Case LIMIT 2';
        return Database.getQueryLocator(query);
    }
}

 
This was selected as the best answer
Alex levy 3Alex levy 3
Credit One Bank provides a variety of purchases and financial benefits and the most important is safety. Many consumers all around the United States can get credit cards from the bank, including MasterCard, American Express, and Visa. Here is the easiest step-by-step guide for a Credit One Login (https://pmyupdate.com/how-to-pay-bill-and-credit-one-login/) or Register to make a payment of your credit card bill.