'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

u_tag is a string field type



an example of something in that field would be:



apple, excel, word, desktop, laptop


OK. Thanks for the sample data and clarification. Whew. If that's the case, then "CONTAINS" is your best candidate for the query operator on u_tag.



Did getRowCount() respond with anything?


Based on my alerts it is throwing the error on this line:



scSearch.addEncodedQuery(scQueryString);


Josh,



addEncodedQuery() method is not available on client side. Use addQuery() instead.



Thanks,


Abhinay




Please mark Helpful, Like, or Correct depending on the impact of the response


Brian Dailey1
Kilo Sage

Hi Josh,



I think you would need to look at splitting your search string (myNewStr) into an array first, and then query on the values using OR conditions.   The problem I see occurring as you have it now, is that as soon as you have multiple values stored in u_tag, then your search string will have to replicate the order exactly in order to match.



      i.e.,


  • if u_tag = "apple, fruit", then searching for:
    • "Show all my fruit apple" will NOT match
    • "Show all my apple fruit" will match
    • "Show all my apple" will NOT match

      Because you will be querying for a u_tag value of "apple, fruit" contained IN "show, all, my, apple" (etc.)



Hope that makes sense...



-Brian