3

I'm trying to build a query builder, where the sObject result can contain an indeterminate number of fields. I'm using the result to build a dynamic table, but I can't figure out a way to read the sObject for a list of fields that were in the query.

I know how to get a list of ALL fields using the getDescribe information, but the query might not contain all of those fields.

Is there a way to do this?

Gabriel Alack
  • 472
  • 2
  • 6
  • 16
  • Have you checked out Apex-Lang? It has a LOT of well-designed dynamic query capability, you don't have to reinvent the wheel. It's the first place to look for lots of this type of utility code. – jkraybill Mar 26 '12 at 01:29

4 Answers4

2

Presumably you're building the query up as a string, since it's dynamic, so couldn't you just loop through the fields in the describe information, and then use .contains() on the query string to see if it was requested? Not crazy elegant, but seems like the simplest solution here.

Taking this further, maybe you have the list of fields selected in a list of strings or similar, and you could just use that list?

Matt Lacey
  • 8,227
  • 35
  • 58
0

Not sure if this is exactly what you were after but something like this?

public  list<sObject>       Querylist               {get; set;}

Define Search String

string QueryString = 'select field1__c, field2__c from Object where';

Add as many of these as you need to build the search if the user searches on these fields

if(searchParameter.field1__c != null && searchParameter.field1__c != '')
    {
        QueryString += ' field1__c like \'' + searchParameter.field1__c + '%\' and ';
    }


if(searchParameter.field2__c != null && searchParameter.field2__c != '')
    {
        QueryString += ' field2__c like \'' + searchParameter.field2__c + '%\' and ';
    }

Remove the last and

QueryString = QueryString.substring(0, (QueryString.length()-4));
      QueryString += ' limit 200';

add query to the list

for(Object sObject : database.query(QueryString))
    {
Querylist.add(sObject);
    }
Mitch Hunt
  • 204
  • 1
  • 5
0

To get the list of fields in an sObject, you could use a method such as:

public Set<String> getFields(sObject sobj) {
    Set<String> fieldSet = new Set<String>();
    for (String field : sobj.getSobjectType().getDescribe().fields.getMap().keySet()) {
        try {
            a.get(field);
            fieldSet.add(field);
        } catch (Exception e) {
        }
    }
    return fieldSet;
}

You should refactor to bulkily this approach for your context, but it works. Just pass in an sObject and it'll give you back a set of the field names.

barelyknown
  • 5,510
  • 3
  • 34
  • 46
0

I suggest using a list of fields for creating both the query and the table. You can put the list of fields in the result so that it's accesible for anyone using it. Then you can construct the table by using result.getFields() and retrieve the data by using result.getRows().

for (sObject obj : result.getRows()) {
    for (String fieldName : result.getFields()) { 
        table.addCell(obj.get(fieldName));
    }
}

If your trying to work with a query that's out of your control, you would have to parse the query to get the list of fields. But I wouldn't suggest trying that. It complicates code in ways that are hard to follow.

Acuariano
  • 498
  • 3
  • 11