'IN' GlideRecord Query Not Working as Expected

codedude
Mega Expert

I have a query that I have written that is looking at a Tag field on the sc_cat_item table. This Tag field is a comma delimited field, for instance it would look like this (apple, laptop, desktop, hp, hardware).

My query looks like this:

var searchParameters = document.getElementById("searchParams").value;

var myNewStr = searchParameters.replace(/ /g,', ');

var scSearch = new GlideRecord('sc_cat_item');

scSearch.addQuery('active', 'true');

scSearch.addQuery('u_tag', 'IN', myNewStr );

scSearch.setLimit(5);

scSearch.orderByDesc('sys_updated_on');

scSearch.query();

If I were to type in "this is my apple" The myNewStr returns this "this, is, my, apple" and I would expect the query to return 1 result, because 1 catalog item has a tag that is named apple.... but it is returning 0 results, why is it not returning anything?

Am I misinterpreting the IN in GlideRecord? I also used CONTAIN and it did not work either...

Any advice?

20 REPLIES 20

Yeap. Totally agree with Brian!


this never makes it to the third alert, so something is wrong with the first or condition i think... Can you see anything?



var scSearch = new GlideRecord('sc_cat_item');


alert('1');


var conditionOne = scSearch.addQuery('u_tag', 'CONTAINS', 'apple');


alert('2');


conditionOne.addOrCondition('u_tag', 'CONTAINS', 'hello');


alert('3');


conditionOne.addOrCondition('u_tag', 'CONTAINS', 'guest');


alert('4');


scSearch.addQuery('active', 'true');


alert('5');


scSearch.query();


Hi Josh,



You're using "alert(...)" and getting results, so I can assume this is a client-side script... no?   Unfortunately, client-side GlideRecord does not include the method addOrCondition(...):


Client Side GlideRecord - ServiceNow Wiki




You could try something like this instead, building your own query string:



var searchParameters = document.getElementById("searchParams").value;  


var arrSearch = searchParameters.replace(/\s/g,',').split(',');  


         


var scSearch = new GlideRecord('sc_cat_item');  


var queryStr = "active=true^u_tagLIKE" + arrSearch[0];


var sep = '^OR';


for(var i = 1;i < arrSearch.length;i++){


queryStr += sep + 'u_tagLIKE' + arrSearch[i];


}


scSearch.addQuery(queryStr);  


scSearch.setLimit(5);  


scSearch.orderByDesc('sys_updated_on');  


scSearch.query()




Give it a try,



-Brian


Chuck Tomasi
Tera Patron

Hi Josh,



I'm going to ask the basic question... what is the business use case for this? Your example appears to be searching for tags - something that has been in ServiceNow for a very long time.



Creating and Using Tags - ServiceNow Wiki



Am I missing something?


I have actually made a custom service catalog that searches the sc_items tag field I added and the knowledge base. The sc_items do not have a tag field out of box..