- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 08:18 AM
In our location table we have Buildings and Floors. Buildings are parents to Floors. Some of our buildings have no child floors.
I'd like to make a catalog UI Policy to show the field "Floor" and make it mandatory only when the field "Building" gets a referenced values that actually has floors.
"When to apply" is easy, when Building=anything. I created the script to fire when this condition is met, but I get Java errors on the form when testing.
function onCondition() {
var floors = new GlideRecord("cmn_location"); //Get locations
var buildingSID = g_form.getValue('scvar_building'); //Get selected bulding's sysID
floors.addEncodedQuery('parent.sys_id='+buildingSID); //Filter for items with a parent of the selected building
floors.query();
if (floors.hasNext()) { //Check to see if query resulted in any items
g_form.setDisplay('scvar_floor', true); //Show Floor
g_form.setMandatory('scvar_floor', true); //Make Floor Mandatory
}
else { //Else if nothing has a parent of the selected building
g_form.setDisplay('scvar_floor', false); //Hide Floor
g_form.setMandatory('scvar_floor', false); //Make Floor Optional
}
}
I haven't done a ton of scripting on UI Policies, feels like im missing something simple.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 08:36 AM
Since a UI Policy script runs on the client-side, it can't execute something like a GlideRecord query (since that's server-side code). If you want to run this check, you'll need to use GlideAjax and contact a script include, passing through the sys_id of the building. Your response from the script include will dictate what you do in the client script (e.g. you would change the floors.hasNext() to something like response == 'success' in your if statement).
If you need help getting started with GlideAjax, check out this comment I left yesterday. You'll have to shift the code a little bit, but the principle is the same.
I believe you'll also want to shift this all to a onChange and/or onLoad client script rather than using a UI Policy. This way, the system can adjust if the selected Building changes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 08:25 AM
Would help to know the precise error you're getting.
GlideRecord has always been a nono for client side operations (UI Policies and Client Scripts). Back in my day they still let you do them but maybe they've made that more restrictive? Anyway, you need it to be more asynchronous. Here's an OOB UI Policy which does something similar you can draw inspiration from.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 08:36 AM
This is the error, not that it helps much:
Nothing seems to pop in the logs to get what is actually erroring.
I'll dig into that UI Policy to see if I can figure out how it's being handled. "GlideAjax" jumps out at me as something I recall from the training courses.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 08:39 AM
Actually, I just did a quick test, it's the encoded query throwing the error. Though something else is wrong because commenting that out, floors.Query() should have resulted in all records, which would have made my IF statement true and showed the field, which still didn't happen.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2024 11:36 AM
Bigger picture here....
Its not a good idea to embed GlideRecord client side.
This is a great opportunity to learn GlideAjax and asynchronous calls.
SLIGHTLY more complicated, but OOB provides a great example.