Syntax Highlighter JS

Monday, December 3, 2012

Visual Force component to generate SOQL where clause

 This is a screen shot of the the "Create new view" page in sales force (standard on any tab).

Having this as a Visual Force component could be used for all kinds of handy things.  In this post, I demonstrate this component and have it return a where clause for SOQL.   It dynamically lists the accessible and filterable fields on the object passed.  Operators are automatically set based on the field type from the schema.

Looking back up at that screen shot one has to wonder, why isn't the list sorted alphabetically?

I think that a possible reason is that the platform lacks a direct way to sort a selection options list.

With permission, I have used a class from Mohammad Usman to sort this control alphabetically.  I have added a test method in my code but his post was found here.

This is an early but working edition.  I hope to eventually fully mirror the native functionality but in the mean time, please be aware that this version has some limitations:
  • It does not have advanced criteria. Right now multiple rows / clauses are and only.
    • Updated on 1/7/2013 to include advanced filtering.
  • Excludes and Includes are not yet supported as operators.
  • DateTime fields (not date fields) must be entered in the SOQL format.
  • No date picker for Date fields. 
    • Updated on 3/25/2013 to include the JQuery UI date picker widget and to reformat the date behind the scenes for SOQL use.
  • Picklist values must already be known as I have yet to add code to list them in a pop-up window like salesforce does.
    • Updated on 1/22/2013 to include a basic look-up window for picklist and name fields.
Since controllers can't directly modify primitives, a wrapper class named SOQLQueryFilterResult is used to leverage the fact that they can modify objects.  This is how the resulting data will be returned.

Here is a sample apex controller:
public with sharing class testControllerExt { public SOQLQueryFilterResult FilterResult {get; set;} public SObject Object2Filter {get; set;} public testControllerExt () { FilterResult = new SOQLQueryFilterResult(); Account MyAccount = new Account(); Object2Filter = MyAccount; } // testControllerExt constructor } // main class
And here is a sample of how the component would be called with that controller:

The full code can be found here.

No comments:

Post a Comment