Creating relationship to not to bring all the records.

TT3
Kilo Guru

I am trying to create a relationship between two tables. However, in the condition script, by default, if the condition doesn't match then ServiceNow brings all the records. We are supposed to show empty list if the it does not match.

Here is the script:

(function refineQuery(current, parent) {

	current.addActiveQuery();
	
// If the order item is null in current table then don't show the list
// so if below condition is satisfied then only it should build relationship and show the 
// list otherwise it should just leave it blank. However, ServiceNow brings all the 
// records from current.

	if (!JSUtil.nil(current.u_order_item)){
	    current.u_order_item = parent.sys_id;	
	}

})(current, parent);
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Instead of using an if statement, use .addNotNullQuery() only get none empty records.

current.addActiveQuery();
current.addNotNullQuery('u_order_item');
current.addQuery('u_order_item', parent.sys_id);

View solution in original post

4 REPLIES 4

Allen Andreas
Administrator
Administrator

Hello,

Please refer to documentation for appropriate formatting: https://docs.servicenow.com/bundle/quebec-platform-user-interface/page/administer/form-administratio...

You would need to properly add to the query, otherwise, it will only add "active" records to the list.

Please read over what you have here and you'll see you are first stating the the records be active, then, if a field isn't null....you're incorrectly setting another parameter...but even if it worked, it would only work when the order item isn't empty.

So documentation says to refine the query like:

current.addQuery('u_order_item', parent.sys_id);

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello,

I'm glad you found a correct answer.

I felt I covered that pretty well above with documentation, explanation as to why what you had now was not working...., and an example for it to work. Unfortunately, not sure what else I could do, heh. It was hopeful that you could take it from there and not need entire script example, but a piece and you could work with it.

Anyways, overall glad it was resolved.

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi, your comments were helpful however, the other answer have given the example of how to address not null scenario too. Thank you.

current.addNotNullQuery('u_order_item');

Hitoshi Ozawa
Giga Sage
Giga Sage

Instead of using an if statement, use .addNotNullQuery() only get none empty records.

current.addActiveQuery();
current.addNotNullQuery('u_order_item');
current.addQuery('u_order_item', parent.sys_id);