Getting operator list based on field display type

Below is the function which you can include in any of the class where you want to return operator list based on field type.


public List getOperatorList(Schema.DisplayType fieldType) {
List operatorList = new List();
operatorList.add(new SelectOption('', '--None--'));

Set stringTypes = new Set();
stringTypes.add(Schema.DisplayType.Reference);
stringTypes.add(Schema.DisplayType.ID);
stringTypes.add(Schema.DisplayType.String);
stringTypes.add(Schema.DisplayType.Email);
stringTypes.add(Schema.DisplayType.URL);
stringTypes.add(Schema.DisplayType.Picklist);
stringTypes.add(Schema.DisplayType.Phone);

Set numberTypes = new Set();
numberTypes.add(Schema.DisplayType.Double);
numberTypes.add(Schema.DisplayType.Percent);
numberTypes.add(Schema.DisplayType.Integer);
numberTypes.add(Schema.DisplayType.Currency);

if(fieldType == Schema.DisplayType.MultiPicklist) {
operatorList.add(new SelectOption(ReadConstantClass.CONTAINS, ReadConstantClass.CONTAINS));
} else if(stringTypes.contains(fieldType)) {
operatorList.add(new SelectOption(ReadConstantClass.EQUALS, ReadConstantClass.EQUALS));
operatorList.add(new SelectOption(ReadConstantClass.CONTAINS, ReadConstantClass.CONTAINS));
operatorList.add(new SelectOption(ReadConstantClass.STARTS_WITH, ReadConstantClass.STARTS_WITH));
operatorList.add(new SelectOption(ReadConstantClass.ENDS_WITH, ReadConstantClass.ENDS_WITH));
} else if(numberTypes.contains(fieldType)) {
operatorList.add(new SelectOption(ReadConstantClass.EQUALS, ReadConstantClass.EQUALS));
operatorList.add(new SelectOption(ReadConstantClass.NOT_EQUALS, ReadConstantClass.NOT_EQUALS));
operatorList.add(new SelectOption(ReadConstantClass.LESS_THAN, ReadConstantClass.LESS_THAN));
operatorList.add(new SelectOption(ReadConstantClass.GREATER_THAN, ReadConstantClass.GREATER_THAN));
operatorList.add(new SelectOption(ReadConstantClass.LESS_OR_EQUALS, ReadConstantClass.LESS_OR_EQUALS));
operatorList.add(new SelectOption(ReadConstantClass.GREATER_OR_EQUALS, ReadConstantClass.GREATER_OR_EQUALS));
} else if(fieldType == Schema.DisplayType.Boolean){
operatorList.add(new SelectOption(ReadConstantClass.EQUALS, ReadConstantClass.EQUALS));
} else if(fieldType == Schema.DisplayType.Date || fieldType == Schema.DisplayType.DateTime){
operatorList.add(new SelectOption(ReadConstantClass.EQUALS, ReadConstantClass.EQUALS));
operatorList.add(new SelectOption(ReadConstantClass.NOT_EQUALS, ReadConstantClass.NOT_EQUALS));
operatorList.add(new SelectOption(ReadConstantClass.LESS_THAN, ReadConstantClass.LESS_THAN));
operatorList.add(new SelectOption(ReadConstantClass.GREATER_THAN, ReadConstantClass.GREATER_THAN));
operatorList.add(new SelectOption(ReadConstantClass.LESS_OR_EQUALS, ReadConstantClass.LESS_OR_EQUALS));
operatorList.add(new SelectOption(ReadConstantClass.GREATER_OR_EQUALS, ReadConstantClass.GREATER_OR_EQUALS));
}
return operatorList;
}

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 ';
}

Published by Sandeep Kumar

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

Leave a Reply