function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
paul.magpaul.mag 

filter by date on visualforce page

I'll try to explain this as best I can but if anything isn't clear please let me know.

 

I am trying to add a field to filter a list of opportunity product line (OPL) items by their start date, we can have OPL for 2012 and the same OPL s for 2013, 2014 etc. Some clients might buy 50+ each year and we need to be abale to apply certain values to just the 2014 OPL. We had a developer build the page and some string filter fields, I have boldly copied the existing work and have the field displaying on the VF page but when I apply the filter I get the error message "like operator only valid on string field". I assume this means that a date field cannot be "Like" anything but actually be that value.

This appears to be the bit of apex that i need to tinker with, could anyone suggest how I shoehorn in a date filter?

 

Thanks in advance

 

/**  
/**
     * Build a list of the required fields, to form the select part of the dynamic SOQL.
     */
    private String buildSelect() {
        
        String selectClause = 'select Id';
        for (String fieldName : tableFields) {
            selectClause += ', ' + fieldName;
        }
        
        return selectClause;
        
    }
    
    
    
    /**
     * Build a series of "and field = 'value'" clauses to append to the SOQL for the dynamic filters.
     */
    private String addFiltering(Map<String, String> filterFields) {
        // If we don't have any filter fields, return immediately.
        if (filterFields == null) {
            return '';
        }
        
        String andClauses = '';
        
        for (String fieldName : filterFields.keyset()) {
            // Escape single quotes on the filter value to avoid SOQL injection.
            String filterValue = String.escapeSingleQuotes(filterFields.get(fieldName));
            andClauses += ' and ' + fieldName + ' like \'%' + filterValue + '%\'';
        }
        
        return andClauses;
    }

*/

 

 

sfdcfoxsfdcfox

Several tidbits here:

 

1) You no longer need to loop through a list like as in buildSelect(). Just do this:

 

return 'select '+string.join(tableFields,',');

2) You need to know the data type of the field. Personally, I would recommend that you try the data field against each type of data until you find the one that matches, or you might perform a describe on the field to obtain the data type. I won't include a full writeup here (it includes a ton of code...), but basically you want to check the field's getDescribe().getType() value against the System.DisplayType enums, as outlined here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_fields_describe.htm.

 

Best of luck with this; it is easy to get lost in the details, but just remember that you need to test the field's type to know what operations are available. In my custom filtering class that's part of our product, I distinguish between "quoted" types (those that need quotes), "equal" types (those that can be compared for equality/inequality), "like" types (those that support the "like" and "not like" keywords), and "comparable" types (those that can be less-than, greater-than, or the equal versions thereof).

paul.magpaul.mag

been messing with this for awhile on and off and think this method is an issue for me as well with the year field being a date but mapped as a string. is there anyway to insert some logic here to to tell the search to look for strings and dates>?

/**
* Performs a search, filtering on the values from the text fields.
*/
public void runSearch() {

Map<String, String> filterFields = new Map<String, String>();
addFilterValue(service, 'Sector__c', filterFields);
addFilterValue(region, 'Region__c', filterFields);
addFilterValue(product, 'PricebookEntry.Name', filterFields);
addFilterValue(year, 'Year__c', filterFields);
addFilterValue(location, 'Location_Site__c', filterFields);

oliList = getLineItems(currentOpp.Id, filterFields);

}

 /**
   * Performs a search, filtering on the values from the text fields.
   */
  public void runSearch() {
    
    Map<String, String> filterFields = new Map<String, String>();
    addFilterValue(service, 'Sector__c', filterFields);
    addFilterValue(region, 'Region__c', filterFields);
    addFilterValue(product, 'PricebookEntry.Name', filterFields);
    addFilterValue(year, 'Year__c', filterFields);
    addFilterValue(location, 'Location_Site__c', filterFields);
    
    oliList = getLineItems(currentOpp.Id, filterFields);
    
  }