Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

REPOST:How to compare two filter conditions

Harshad Wagh
Tera Guru

 

Is there any way to compare two filter conditions in order to find match.

e.g

var filterCondition1 = 'active=true^priority=1^ORcategory=hardware';

var filterCondition2 = 'priority=1^active=true^ORcategory=hardware';

 

above conditions are same.

 

consider a scenario, i want to block devs to create duplicate Business rules on incident table where there is already a existing BR with similar condition. (this is just example).

 

now that condition match can happen even those conditions are in different order or such as text match

 

Thanks

Harshad

 

4 REPLIES 4

Harshal Aditya
Mega Sage

Hi @Harshad Wagh ,

 

Hope you are doing well.

 

Please refer below code that I executed form background script.

Considering we are comparing for incident table.

 

var filterCondition1 = 'active=true^priority=1^ORcategory=hardware';
var filterCondition2 = 'active=true^priority=1^ORcategory=hardware';
var arr1 = [];
var arr2 = [];
var getIncident1 = new GlideRecord("incident");
getIncident1.addEncodedQuery(filterCondition1);
getIncident1.query();
while(getIncident1.next()){
	arr1.push(getIncident1.number.toString());
	}
	var getIncident2 = new GlideRecord("incident");
getIncident2.addEncodedQuery(filterCondition2);
getIncident2.query();
while(getIncident2.next()){
	arr2.push(getIncident2.number.toString());
	}
gs.info(arr1);
gs.info(arr2);
var arrayUtil = new ArrayUtil();
if(!arrayUtil.diff(arr1, arr2)){
gs.info("Both the filters are same")
}
else{
gs.log("Both the filters are different with difference"+ arrayUtil.diff(arr1, arr2))
}

 

Please do let me know if you find anything OOB 🙂

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

 

Harshad Wagh
Tera Guru

thanks for the code but this is when there is any incident existing matching with both criteria, I am looking for logic even there cant be any record, it may be in future.

 

e.g.

 

on condition is short_description = 123 and other is  short_descriptionLIKE123 both are same.

 

so there can be combinations of this conditions with AND OR different fields etc.

 

Thanks

Harshad

 

 

jMarshal
Mega Sage

Query optimization is a pretty extensive topic and I don't believe there's an easy solution using in-platform techniques, without writing a monster function or multiple functions in a script include.

There are external products and services that do query optimization that you could employ (Apache Calcite for instance) - and you could have a business process for your developers to use that before writing BRs...?


Also, keep in mind that by writing this in-platform (instead of adopting development business practice separately) you may be going down a "rabbit hole"....consider where "created_by" and "opened_by" are the same person - do you want to prevent "duplicate" filter conditions like

 

var filterCondition1 = 'active=true^priority=1^ORcreated_by="Billy"';

 

is same as

 

var filterCondition2 = 'active=true^priority=1^ORopened_by="Billy";

??

Prateek kumar
Mega Sage

How about?

var filterCondition1 = 'active=true^priority=1^ORcategory=hardware';
var filterCondition2 = 'priority=1^active=true^ORcategory=hardware';

var str1 = filterCondition1.split('').sort().join('');
var str2 = filterCondition2.split('').sort().join('');
gs.info(str1);
gs.info(str2);

if(str1 == str2){
gs.info('True');
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks