What are some reasons that two different reference fields referencing the same table with the same value would not match on a "!=" or a "DOES NOT CONTAIN" GlideRecord query operator?

bcronrath
Kilo Guru

I've been trying to do some cleanup due to an integration that used a custom company field instead of the default one on the incident table.   I'm trying to find the places where company value is different between the two fields where they are not null.   They are both reference fields referencing the core_company table.   I am running into some confusing behavior.   I have noticed that using either the != or DOES NOT CONTAIN operator is giving me only incidents where the company does in fact actually match the sys_id value from the core_company table.   When I use the = or CONTAINS operator, I get no results.   I'm completely stumped right now as to why this is.   I have double and triple checked that the values that are output from this gs.print are exactly the same.   Clearly something in the system considers them to be different.   Here is my script:

var gl = new GlideRecord('incident');

gl.addQuery('u_customer_type','external');

gl.addQuery('company','!=','u_company');

gl.addNotNullQuery('company');

gl.addNotNullQuery('u_company');

gl.query();

while(gl.next()){

  gs.print(gl.number + " Company: " + gl.company + " u_company: " + gl.u_company);

}

Does anyone have any thoughts?

Best regards,

Brian

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

Please try the following:



var gl = new GlideRecord('incident');


gl.addQuery('u_customer_type','external');


gl.addEncodedQuery('companyNSAMEASu_company');


gl.addNotNullQuery('company');


gl.addNotNullQuery('u_company');


gl.query();


while(gl.next()){


  gs.print(gl.number + " Company: " + gl.company + " u_company: " + gl.u_company);


}



As I understand it, your query was saying, in English



SYS ID of COMPANY is not equal to "u_company"



which of course is wrong ! Alas, you were getting sys_ids that were the same!



NSAMEAS is "is different"



IS DIFFERENT.png



If ever having troubles with queries, do it in a list then copy the query to clipboard.


Then, write you code and output 'current.getEncodedQuery()'



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

5 REPLIES 5

It is a bit odd because technically in my example above, yeah 324j23h4jk32gj4h32 was the exact same string that was being stored in u_company.   However, I think the FIELD, OPERATOR, VALUE is key here, because it seems that even though if I output each field's value as a string and they are the same, something about the way the data types are stored (field versus value) is causing a mismatch.   But the encoded query gets around this.