Skip to main content
added 1660 characters in body
Source Link
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.countQueryQuery('SELECT Count' +allFields+ 'Id ' + ' FROM Account');
            System.debug(s);
 FROM           
            for(Account WHEREacc ': +fieldName+s ') {
                for(String fieldName : fieldList){ 
                    if(acc.get(fieldName) != NULL'null); {
               // system        if(recordListbyFieldMap.debugcontainskey('NotfieldName)) Null{
 Fields->>' +s                      recordListbyFieldMap.put(fieldName,recordListbyFieldMap.get(fieldName) + 1);
               //         }
                         else {
                        recordListbyFieldMap.put(fieldName,1); 
                        }
                    }
                }
            }
            system.debug('fieldName->>'+fieldName'count accounts where Annual Revenue is not null' + recordListbyFieldMap.get('AnnualRevenue'));
       }
  }
Integer s = database.countQuery('SELECT Count() FROM Account WHERE ' +fieldName+ ' != NULL'); 
               // system.debug('Not Null Fields->>' +s);
               // system.debug('fieldName->>'+fieldName); 
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'));
       }
  }
edited tags
Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431
Became Hot Network Question
Source Link

Unable to escape the governor limits (Too Many SOQL Queries: 201)

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 ?

Integer s = database.countQuery('SELECT Count() FROM Account WHERE ' +fieldName+ ' != NULL'); 
               // system.debug('Not Null Fields->>' +s);
               // system.debug('fieldName->>'+fieldName); 

here fieldName is having all the field name (350+) which Iam using individually inside a loop (i know it's a bad practice)