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
Shriwastab SinghShriwastab Singh 

How to fetch Object's all fields which value is not null .Is it possible by apex SOQL .

Hi
I want to fetch all fields which values is not null by  SOQL like (Select * from ObjectName)
exp - Object A have 10 fields
In Obj A has 3 fieds values null and 7 is not null.So I fetch all not null vaues by SOQL
Best Answer chosen by Shriwastab Singh
Akshay_DhimanAkshay_Dhiman

Hi Sriwastab,

Try this and for an exception, You can not filtred Address=null so plz skip this fields.becz address fields are concate with four fields

(Street,City,State,Country)
 String query ='';
 String SobjectApiName = 'Account';
 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
 Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

 String strFields = '';
String conWhere ='';
 
 for(String fieldName : fieldMap.keyset() )
     
 {
  if(strFields == null || strFields == '')
  {
   strFields = fieldName;
   conWhere = fieldName+'!=null';
  }else{
   strFields = strFields + ' , ' + fieldName;
      if(fieldName =='BillingAddress' || fieldName =='ShippingAddress' || fieldName=='description')
      {
          System.debug(fieldName);
           
      }else{
           System.debug(fieldName);
          conWhere = conWhere +' '+ 'OR '+' '+fieldName+'!=null';
      }    
  }
 }
System.debug('conWhere-->'+conWhere);

 query = 'select ' + strFields + ' from ' + SobjectApiName + ' Where '+conWhere+' Limit 5 ';

 List <Account> accList = Database.query(query);
System.debug('accList'+accList);
 


if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay

All Answers

Akshay_DhimanAkshay_Dhiman
 Hi Sriwastab,

 try this code I hope is very helpful for you.
 
String query ='';
 String SobjectApiName = 'Account';
 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
 Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

 String strFields = '';
String conWhere ='';
 
 for(String fieldName : fieldMap.keyset() )
     
 {
  if(strFields == null || strFields == '')
  {
   strFields = fieldName;
   conWhere = fieldName+'!=null';
  }else{
   strFields = strFields + ' , ' + fieldName;
   conWhere = conWhere +' '+ 'OR '+' '+fieldName+'!=null';
       
  }
 }
System.debug('conWhere-->'+conWhere);

 query = 'select ' + strFields + ' from ' + SobjectApiName + ' Where '+conWhere+' Limit 5 ';

List <Account> accList = Database.query(query);
System.debug('accList'+accList);

if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay
  
Shriwastab SinghShriwastab Singh
Thansk Aksay for quick responce . I am try this code but give an error
System.QueryException: Address fields can only be filtered using Distance expressions
Akshay_DhimanAkshay_Dhiman

Hi Sriwastab,

Try this and for an exception, You can not filtred Address=null so plz skip this fields.becz address fields are concate with four fields

(Street,City,State,Country)
 String query ='';
 String SobjectApiName = 'Account';
 Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
 Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

 String strFields = '';
String conWhere ='';
 
 for(String fieldName : fieldMap.keyset() )
     
 {
  if(strFields == null || strFields == '')
  {
   strFields = fieldName;
   conWhere = fieldName+'!=null';
  }else{
   strFields = strFields + ' , ' + fieldName;
      if(fieldName =='BillingAddress' || fieldName =='ShippingAddress' || fieldName=='description')
      {
          System.debug(fieldName);
           
      }else{
           System.debug(fieldName);
          conWhere = conWhere +' '+ 'OR '+' '+fieldName+'!=null';
      }    
  }
 }
System.debug('conWhere-->'+conWhere);

 query = 'select ' + strFields + ' from ' + SobjectApiName + ' Where '+conWhere+' Limit 5 ';

 List <Account> accList = Database.query(query);
System.debug('accList'+accList);
 


if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay

This was selected as the best answer
Shriwastab SinghShriwastab Singh
ThankYou aksay its working fine