Getting Where condition string for filtering specific object records

Below is the function and wrapper class which can be used to get where condition. This function can be customized as per different requirement.


//Returns wherecondition for soql based on SearchFieldWrapper class list which can be used on visual force page for filter functionality.
public String getWhereCondition (List searchFieldWrapperList) {
String whereCondition = '';
for (WrapperClass f : searchFieldWrapperList) {
String fieldValue = f.fieldValue;
String fieldName = f.fieldName;
if(f.fieldName != null && f.ObjectName != null) {
Schema.DisplayType fieldDisplayType = SchemaCache.getFieldDescribe(obj, fieldName).getType();
if(fieldDisplayType == Schema.DisplayType.Reference) {
if(f.fieldName.contains('__c')) {
fieldName = f.fieldName.replace('__c', '__r') + '.Name';
} else if(f.fieldName.endswith('id')){
fieldName = f.fieldName.substring(0, f.fieldName.length()-2)+'.Name';
}
}
if(!String.isBlank(fieldValue) && f.customFieldType == 'str') {
if(f.selectedCondition == ReadConstantClass.CONTAINS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' LIKE \'' + '%' + fieldValue + '%\' ';
} else if(f.selectedCondition == ReadConstantClass.STARTS_WITH) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' LIKE \'' + fieldValue + '%\' ';
} else if(f.selectedCondition == ReadConstantClass.ENDS_WITH) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' LIKE \'' + '%' + fieldValue + '\' ';
} else if(f.selectedCondition == ReadConstantClass.EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' = \'' + fieldValue + '\' ';
}
} else if(f.customFieldType == 'num') {
if(String.isBlank(fieldValue)) {
fieldValue = '0';
}
if(fieldValue.isNumeric()) {
if(f.selectedCondition == ReadConstantClass.EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' = ' + fieldValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.NOT_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' != ' + fieldValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.LESS_THAN) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' < ' + fieldValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.GREATER_THAN) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' > ' + fieldValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.LESS_OR_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' <= ' + fieldValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.GREATER_OR_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' >= ' + fieldValue + ' ';
}
} else {
returnBlankList = true;
}
} else if(f.customFieldType == 'bool') {
if(f.selectedCondition == ReadConstantClass.EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' = ' + f.boolValue + ' ';
}
} else if(f.dateValue != null && f.customFieldType == 'dateType') {
String dateValue = String.valueOf(f.dateValue).substring(0, 10);
if(f.selectedCondition == ReadConstantClass.EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' = ' + dateValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.NOT_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' != ' + dateValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.LESS_THAN) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' < ' + dateValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.GREATER_THAN) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' > ' + dateValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.LESS_OR_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' <= ' + dateValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.GREATER_OR_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' >= ' + dateValue + ' ';
}
} else if(f.dateTimeValue != null && f.customFieldType == 'dateTimeType') {
String dateTimeValue = f.dateTimeValue.format('yyyy-MM-dd\'T\'hh:mm:ss\'z\'');
if(f.selectedCondition == ReadConstantClass.EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' = ' + dateTimeValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.NOT_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' != ' + dateTimeValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.LESS_THAN) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' < ' + dateTimeValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.GREATER_THAN) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' > ' + dateTimeValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.LESS_OR_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' <= ' + dateTimeValue + ' ';
} else if(f.selectedCondition == ReadConstantClass.GREATER_OR_EQUALS) {
whereCondition += ReadConstantClass.AND_OPERATOR + fieldName + ' >= ' + dateTimeValue + ' ';
}
}
}
}
return whereCondition;
}

//Wrapper Class
public class SearchFieldWrapper {
public String fieldName {get;set;}
public String fieldValue{get;set;}
public Boolean boolValue {get;set;} //In case of boolean type, use this value rather than fieldValue
public Date dateValue {get;set;} //In case of date type, use this value
public DateTime dateTimeValue {get;set;} //In case of date time type, use this value
public String selectedCondition {get;set;}
public String customFieldType{get;set;} // str, num, bool, dateType, dateTimeType
}

ReadConstantClass class


public class ReadConstantClass{
public static final string EQUALS = 'equals';
public static final string NOT_EQUALS= 'not equal to';
public static final string LESS_THAN = 'less than';
public static final string GREATER_THAN = 'greater than';
public static final string LESS_OR_EQUALS= 'less or equal';
public static final string GREATER_OR_EQUALS = 'greater or equal';
public static final string CONTAINS = 'contains';
public static final string STARTS_WITH = 'starts with';
public static final string ENDS_WITH = 'ends with';
public static final string ORDER_BY = ' order by ';
public static final string WHERE_CLAUSE = ' where ';
public static final string IN_CLAUSE = ' in ';
public static final string NOT_OPERATOR = ' not ';
public static final string AND_OPERATOR = ' and ';
public static final string LIMIT_ROWS = ' not ';
}

Feel free to contact me regarding full implementation.

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply