0

I stored all the editable fields in a variable and calling the variable in a dynamic SOQL query. My Query:

String fieldApiList = String.join(fieldsApiNames, ',');
        query = 'SELECT Name,'+ fieldApiList +' from EP_Service_License__c WHERE ID IN:recordIdSet';
        
        SelectedEpserviceRecords=Database.query(query);  

Now, using a VF page I am displaying the fields and its values like as follows enter image description here

my requirement is, if any field value is null or blank in the selected records or any field values is same in all the selected records then I should not display that field. For example, here in the picture, product license number is same for all the selected records and Copy Primary Prod Co License(s) field is null for all the selected records. Now, I should avoid displaying those two in the output. This should happen dynamically for all the fields.

Kindly help me on this

2 Answers 2

1

With your fieldApiNames, you can check each field to see which values are returned:

String[] fieldsToDisplay = new String[0];
for(String fieldName: fieldApiNames) {
  Set<Object> values = new Set<Object>();
  for(sObject record: SelectedEpserviceRecords) {
    values.add(record.get(fieldName));
  }
  if(values.size() > 1) {
    fieldsToDisplay.add(fieldName);
  }
}

Then you'll be able to show only the fields that have at least two different values.

0

you can try this:

    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    Schema.SObjectType EP_Service_License__cSchema= schemaMap.get(EP_Service_License__c);

    Map<String, Schema.SObjectField> lfieldMap = EP_Service_License__cSchema.getDescribe().fields.getMap();
    String fieldApiList = String.join(fieldsApiNames, ',');
    query = 'SELECT Name,'+ fieldApiList +' from EP_Service_License__c WHERE ID IN:recordIdSet';
    for (String field : fieldApiNames) {
        Schema.DisplayType lfielddataType = lfieldMap.get(field).getDescribe().getType();
        if (lfielddataType == DisplayType.String) { // Check for other convenient types subjected to be ''
            query += ' AND ' + field + ' != \'\'
        }
        query += 'AND ' + field + ' != null';
    }
    
    SelectedEpserviceRecords=Database.query(query);  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.