1

i Designed my own Salesforce Advanced Search with filter criteria but i'm unable to Validate filter Criteria of "(1 AND 2) OR 3", if it is validated then my advanced search work perfectly, how can i implement validator like salesforce.

like standard salesforce functionality in report.

i tried some java-script but it is unstable. if any suggestions please post it.

enter image description here

1
  • Can you post the solution for Custom logic filter validation part
    – SFDC
    May 23, 2022 at 9:06

2 Answers 2

5

This is solution for input text area to define logically expression is valid or not. It is work fine for me for any number of criteria rows.

function validateLogic(a){ 
    a = new filterValidate.Parser(a);
    for (var b = [], c = 0; c < rowsSize; c++) b.push(c + 1);
    try {
        return a.getFiltersInUseMessage(b)
    } catch (e) {
        return e.message ? e.message : LC.getLabel("ReportBooleanFilter", "error_generic")
    }
}


filterValidate = {
LPAREN_ID: 0,
RPAREN_ID: 1,
AND_ID: 2,
OR_ID: 3,
NOT_ID: 4,
INTEGER_ID: 5,
LPAREN: function() {
    this.id = filterValidate.LPAREN_ID
},
RPAREN: function() {
    this.id = filterValidate.RPAREN_ID
},
AND: function() {
    this.id = filterValidate.AND_ID;
    this.operation = "AND"
},
OR: function() {
    this.id = filterValidate.OR_ID;
    this.operation = "OR"
},
NOT: function() {
    this.id = filterValidate.NOT_ID;
    this.operation = "NOT"
},
INTEGER: function(a) {
    this.id = filterValidate.INTEGER_ID;
    this.value = parseInt(a, 10)
},
TokenParsingException: function() {
    this.message = LC.getLabel("ReportBooleanFilter", "error_typo")
},
ParenMismatchException: function() {
    this.message = LC.getLabel("ReportBooleanFilter", "error_typo")
},
TooManyValuesException: function() {
    this.message = LC.getLabel("ReportBooleanFilter", "error_typo")
},
UnexpectedTokenException: function() {
    this.message = LC.getLabel("ReportBooleanFilter", "error_typo")
},
EmptyInputException: function() {
    this.message = LC.getLabel("ReportBooleanFilter", "requiredFieldError")
},
MissingOperandException: function(a) {
    this.message = LC.getLabel("ReportBooleanFilter", "error_typo")
},
EndOfTokensException: function() {
    this.message = LC.getLabel("ReportBooleanFilter", "error_typo")
},
Parser: function(a) {
    this.input = a
},
Node: function(a, b, c) {
    this.left = a;
    this.op = b;
    this.right = c
}};

This is Saleforce standard filter logic criteria. Call validateLogic() this will return error message if logically not validated.

2
  • where is "getFiltersInUseMessage(b)" and "LC.getLabel()" function. so i can use it. i think i'm on right track. it seems working. Oct 26, 2018 at 10:42
  • It's Report Builder JS function of Saleforce. 1) "../analytics-reporting-impl/ReportsCore.js". 2) "../analytics-reporting-impl/ReportFilterPanel.js". 3) "../analytics-reporting-impl/ReportBuilder.js". You can find it in your org Report page source. Oct 26, 2018 at 10:55
1

You can use String.format() see example below:

String placeholder = '({0} AND {1}) OR {2}';
List<String> fillers = new String[]{'Field1__c','Field2__c','Field3__c'};
String formatted = String.format(placeholder, fillers);
System.assertEquals('(Field1__c AND Field2__c) OR Field3__c', formatted);
String wholeQuery = 'SELECT Id FROM Your_Object__c WHERE '+formatted;
try{
    Database.query(wholeQuery);
}catch(Exception e){
    //place your page message here
}

If it goes to catch than return a page message which basically tells, the filter logic is not valid.

3
  • thanks umithuckan for reply, but here concern is if i type "(1 OR 2) AND 3" that is right but if i type "(1 OR 2) AND OR" which is incorrect so fire validation error like "Error:Check the spelling in your filter logic." Oct 26, 2018 at 9:38
  • 1
    I"d suggest appending a LIMIT 1 to the query to prevent long wait times and/or hitting limits. Oct 26, 2018 at 9:39
  • You need to place apex:messages tag in your page and on the catch you can return "Check the spelling in your filter logic" message in the apexpages.message. So, let's say you are calling this action via ActionFunction, then basically rerender your apex:messages tag via ActionFunction.
    – umithuckan
    Oct 26, 2018 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .