Saturday 21 February 2015

Dynamically Retrieve All fields in SOQL without Mentioning the Field Name in Salesforce


Here is the way to query the all field’s values without mentioning the Field name.


  1. // Specify the Object Name to retrieve the List of field names.
  2. Map<String, Schema.SObjectField> fieldMap = Account.sObjectType.getDescribe().fields.getMap();
  3.        
  4. String fieldName = '';
  5. String accountQuery;
  6.        
  7. for(Schema.SObjectField currentField : fieldMap.Values())
  8. {
  9.      Schema.DescribeFieldResult fieldResult = currentField.getDescribe();  
  10.      fieldName += fieldResult.getName() + ',';
  11. }
  12.        
  13. fieldName = fieldName.substring(0, fieldName.length()-1);
  14. // Build a Dynamic Query String.
  15. accountQuery = 'SELECT ' + fieldName + ' FROM Account';
  16. List<Account> accList = Database.query(accountQuery);


Reference:


No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...