My governor limits are getting exceeded because i need to use count() for each individual fieldName and check fieldName != null, and return it's individual value
But there are over 300 fields, how to do it within governor limits? Is there any alternate approach to query each field name individually ?
public class testAccFields {
public void testAccFields1() {
String sobjectApiName = 'Account';
Integer count = 0;
String runQuery = '';
String test = '';
String allFields = '';
List<String> fieldList = new List<String>();
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
Map<String,Integer> recordListbyFieldMap = new Map<String,Integer>();
for(String fieldName : fieldMap.keyset() )
{
if(fieldName == 'Id' || fieldName == 'bank_account_number__c' || fieldName == 'BillingAddress' || fieldName == 'ShippingAddress' || fieldName== 'description' || fieldName == 'BillingStreet')
{
System.debug(fieldName);
}
else {
test += +fieldName+ '!= NULL OR ';
allFields += +fieldName+ ',';
fieldList.add(fieldName);
}
}
List<Account> s = database.Query('SELECT ' +allFields+ 'Id ' + ' FROM Account');
System.debug(s);
for(Account acc : s ) {
for(String fieldName : fieldList){
if(acc.get(fieldName) != null) {
if(recordListbyFieldMap.containskey(fieldName)) {
recordListbyFieldMap.put(fieldName,recordListbyFieldMap.get(fieldName) + 1);
}
else {
recordListbyFieldMap.put(fieldName,1);
}
}
}
}
system.debug('count accounts where Annual Revenue is not null' + recordListbyFieldMap.get('AnnualRevenue'));
}
}
here fieldName is having all the field name (350+) which Iam using individually inside a loop (i know it's a bad practice)