Scripted reference qualifier on unsaved record

MC1
Tera Contributor

I have a field called u_mission_selector that should be dependent on the value for u_node_selector. u_mission_selector references a table called u_request. u_request has a reference field called u_node, which references the same u_request_node table that u_node_selector references. I need to filter u_mission_selector based on the choice made for u_node_selector.

Here is the reference qualifier I have for u_mission_selector.

javascript: new RequestSelectorUtils().getMissionsForNode(current.u_node_selector)



And here is my scripted function:

    getMissionsForNode: function(node) {
		gs.addInfoMessage('NODE: ' + node);
		if (JSUtil.nil(node)) {
			return;
		}
		var missionList = [];
		var gr = new GlideRecord('u_request');
		gr.addQuery('u_node', node);
		gr.addNotNullQuery('u_mission_number');
		gr.query();
		while (gr.next()) {
			missionList.push(gr.sys_id);
		}
        return 'sys_idIN' + missionList.join(',');
    },


After logging out the value of node, what I realize is that the node isn't being passed into the function unless the form has already been saved with that value.

It absolutely CANNOT work this way per client requirements. The reference qualifier needs to be able to work without the value of u_node_selector needing to be saved. I need a way to filter based on the currently selected value rather than the currently saved value. Can anyone help me with this?

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

I'm not observing that behavior.  In my PDI as a more or less out of the box example on the incident form I added a reference qualifier on Service offering, passing in the current.service to a SI.  In my SI I log the argument value passed into the function, and I can see it when creating a new incident, before saving the record.  I'm just clicking New on the Incident list, supplying a Service, then clicking the search icon beside Service offering and there is an info message with the sys_id of the service that I selected.  Is your Script Include Client callable?

The script include is client callable and I can confirm it is being called. However, it's getting a null value for node, even though a value is being selected. The problem is that it seems to be expecting a saved value rather than a selected value.

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @MC1 ,

Try this below code in your script include:-

 getMissionsForNode: function(node) {
		gs.info('NODE: ' + node);
		if (JSUtil.nil(node)) {
			return;
		}
		var missionList = [];
		var gr = new GlideRecord('u_request');
		gr.addQuery('u_node', node);
		gr.addNotNullQuery('u_mission_number');
		gr.query();
		while (gr.next()) {
			missionList.push(gr.sys_id.toString());
		}
        return 'sys_idIN' + missionList;
    },

After updating the above code in script include then test it. If it doesn't work then you can go to system log search of node in message and check if you are getting the value of node or not.

Also, as @Brad Bowman  said that your script include should be client callable if it's not then check the check box for client callable.

 

If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks,

Alka

Jim Coyne
Kilo Patron

Couple things.

 

First, unfortunately, @Alka_Chaudhary's edit was not obvious but is VERY important and should have been pointed out.  When pushing data into a Array, you must ensure you are adding a JavaScript primitive type (in this case a string) and not a GlideRecord object that can change as you loop though some records.  Check out this article: TNT: The Importance of Using "getValue()" when Getting Data from a GlideRecord for more detailed information. That's why I really don't like "do this" answers, as they don't teach anything.

 

Second, it looks like your reference qualifier can be simplified so it does not use a Script Include.  I've setup a quick little example to simulate your requirements.  I have 2 reference fields, 1 to User and the other to the Request table.  If I select "System Administrator" as the User, I get the following Requests to choose from:

 

JimCoyne_0-1697661332904.png

 

And if I select myself, I see a different list:

 

JimCoyne_1-1697661461909.png

 

Here's the Reference Qualifier:

JimCoyne_2-1697661571459.png

 

Here's the code part so you can copy and modify it:

 

 

javascript: "requested_forISNOTEMPTY^requested_for=" + current.getValue("user")

 

 

 

I think your Reference Qualifier could/would look like:

 

 

javascript: "u_mission_numberISNOTEMPTY^u_nodeISNOTEMPTY^u_node=" + current.getValue("u_node_selector")

 

 

 

For an easy way to create an encoded query take a look at this SN Docs article: Generate an encoded query string through a filter