Script include for a reference qualifier help

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2024 07:25 PM - edited 08-14-2024 12:31 AM
So I need to have a script include in order to cover a couple of scenarios for an advanced filter on a reference variable field on an incident record producer.
Ultimately what I want to do is make it so if a user selects 'Room' in the issue field and 'second floor' in the floor field, then the Location select box will only show second floor rooms.
Likewise if they choose 'Desk' and 'Ground Floor' then only the ground floor desks are shown.
But then if they choose anything else as an issue (eg, plumbing or air conditioning) then everything is shown for the floor they choose.
Hope that makes sense?
The variable fields I have are:
Variable Name | Field type | Option names |
u_rec_issue | select box | Air Conditioning |
Desks | ||
Rooms | ||
Plumbing |
Variable Name | Field type | Option names |
u_rec_floor | select box | Ground Floor |
First Floor | ||
Second Floor | ||
Basement |
Variable Name | Field type | Field name | Choices |
u_rec_location | reference | cnm_location_type | room |
Location [cmn_location] | desk |
Would really love help please - I have limited experience writing a script include and am not even sure how to tell it to look at the 'current' record calling it and to use the values from the variables on that record producer.
Much appreciate the assistance thanks 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2024 08:02 PM
Hi @Bidduam,
Write an onChange catalog client script on u_rec_issue variable.
if(g_form.getValue('u_rec_issue') == '<rooms>'){
if(g_form.getValue('u_rec_floor') == '<floorvalue1>'){
g_form.clearOptions('u_rec_location');
//using g_form.addOption() try to add the options you want to see.
} else if(g_form.getValue('u_rec_floor') == '<floorvalue2>'){
g_form.clearOptions('u_rec_location');
//using g_form.addOption() try to add the options you want to see.
}
} else if(g_form.getValue('u_rec_issue') == '<airconditioning>' || g_form.getValue('u_rec_issue') == '<plumbing>' ){
g_form.clearOptions('u_rec_location');
//using g_form.addOption() try to add the options you want to see.
}
The above one is just a sample code. Try to change the values accordingly and use it.
You can use g_form.getValue() - to get the selected value from the form after changing the value.
g_form.clearOption() - clears the options in the dropdown.
g_form.addOption() - adds the options to the dropdown.
Mark the response correct and helpful if the answer assisted your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2024 08:42 PM
@Rupanjani I understand the concept of getting the values from the other variables and using them in if statements, but how do I use those values I get in the catalog client script to filter the u_rec_location field?
Where am I setting the filter / reference qualifier for u_rec_location?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2024 12:33 AM
Anyone know how I'd do this with the script include please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2024 01:11 AM
Hi @Bidduam
As per your scenario you require script include and client script for this solution. Have a look of below script include & client script:
Script include -
var LocationFilter = Class.create();
LocationFilter.prototype = {
initialize: function() {},
// Method to filter locations based on issue and floor
getLocations: function(issue, floor) {
var locations = [];
var locationGR = new GlideRecord('cmn_location');
// Add query conditions based on issue and floor
locationGR.addQuery('u_floor', floor); // Assuming u_floor is a custom field on cmn_location
if (issue == 'Rooms') {
locationGR.addQuery('u_type', 'room');
} else if (issue == 'Desks') {
locationGR.addQuery('u_type', 'desk');
}
// If it's a different issue, all locations on the selected floor should be shown
locationGR.query();
while (locationGR.next()) {
locations.push({
sys_id: locationGR.getValue('sys_id'),
name: locationGR.getValue('name')
});
}
return locations;
},
type: 'LocationFilter'
};
OnChange Client script -
Variable name: u_rec_issue, u_rec_floor (use comma to separate multiple fields)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Get the values of the fields
var issue = g_form.getValue('u_rec_issue');
var floor = g_form.getValue('u_rec_floor');
// Call Script Include to get filtered locations
var ga = new GlideAjax('LocationFilter');
ga.addParam('sysparm_name', 'getLocations');
ga.addParam('issue', issue);
ga.addParam('floor', floor);
ga.getXMLAnswer(function(response) {
var locations = JSON.parse(response);
var options = g_form.getField('u_rec_location').options;
// Clear existing options
for (var i = options.length - 1; i >= 0; i--) {
options[i].remove();
}
// Add default option
g_form.addOption('u_rec_location', '', '-- Select a Location --');
// Add filtered locations
locations.forEach(function(location) {
g_form.addOption('u_rec_location', location.sys_id, location.name);
});
});
}
I hope my answer helps you to resolve your issue, if yes please mark my answer correct & helpful.
thank you
rajesh