How to generate dynamic queries with multipicklist - provide data as Range of Values

by - 4:15 PM

Couple of weeks before I shared piece of code snippet where I explained how to generate dynamic query with multipicklist using Jquery.

Here is another deal I countered where I was given with Multipicklist but this time the  values in picklist are range of values rather than exact values to match, something similar shown here
  <- Range of Values

The idea was, when user select a value in the list, the SOQL query should be dynamically generated as

To get range of value from Multipicklist- I designed these two methods to help myself and I thought of sharing this solution with reader out here

Method 1 : formatMultiSelect()

   <- Exact Values    

This method will pick value chosen from multiSelect and generate formatted SOQL queries. For example, If we choose Expired and Prospect the resulting query will look this
Now the tricky part is handling range of values and generate appropriate SOQL by applying range as limits, to resolve this, I break down problem as in these steps
  • Let us format selection with comma separated values first using formatMultiSelect() method  like  '1001', '2500'
  • Now break string by removing comma (result : '1001'   '2500')
  • Remove Single Quotes (like 'Expired')  (result : 1001 2500)
  • Split it back based on even/odd index, if odd index use (greater than) sign if even index then use  < (less than) sign  ->  (example  - Amout > 1001 and Amount < 2500)
  • Rejoin the array

Method 2 : getMultipleRanges()

How to Implement Method 

You can generate dynamic SOQL to get the query values here, this code implements the methods I designed and generate dynamic SOQL which in turn query salesforce with formatted string

You May Also Like