'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

Chuck Tomasi
Tera Patron

Hi Josh,



Have you tried removing the extra space after the ',' in your replace? That space may be messing with things.



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


bernyalvarado
Mega Sage

Hi Josh,



Have you tried using EncodedQuery?



on your case it will be something like:



scSearch.addEncodedQuery('active=true^u_tagIN' + myNewStr)



Thanks,


Berny


Something that I really like about Encoded queries is that you can copy/paste the base encoded query from the filter breadcrumb of your table's list view.



Thanks,


Berny


So it now looks like this:



var scQueryString = "active=true^u_tagLIKE" + myNewStr;


var scSearch = new GlideRecord('sc_cat_item');


scSearch.addEncodedQuery(scQueryString);


scSearch.query();



while(scSearch.next())


{


          alert('found query results');


}



var scQueryString = "active=true^u_tagIN" + myNewStr;


var scSearch = new GlideRecord('sc_cat_item');


scSearch.addEncodedQuery(scQueryString);


scSearch.query();



while(scSearch.next())


{


          alert('found query results');


}



IT did not hit the alert for either query...