Help with advanced reference qualifier script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 09:24 PM
Hi all,
Thank you for a great forum! I am new to scripting, and having trouble wrapping my head around advanced reference qualifiers with script includes.
I have 3 reference variables (all references to kb_category table):
1. Service (u_service)
2. Subservice (u_subservice)
3. Topic (u_topic)
Variable 2: Subservice should show child kb categories of the current selected option in Variable 1: Service. And Variable 3. Topic should show child kb categories of the current selected option in Variable 2.
I am trying to achieve this through Advanced reference qualifiers calling for script include called GetKBcategories.
I am having trouble with the script and using glide record query.
I understand I should be using something like this:
var gr = new GlideRecord('kb_category');
gr.addQuery('x',x);
gr.query();
grList = gr.sys_id;
AND / OR something like this example:
var usr = new GlideRecord('sys_user');
usr.get(userID);
var userDepartment = usr.department;
var depUsers = new GlideRecord('sys_user');
depUsers.addquery('department', userDepartment );
depUsers.query();
var users = []
while (depUsers.next()){
users.push(depUsers.sys_id.toString());
OR could something like this work:
var kbcat = current.variables.u_kb_service.getValue();
So as a beginner I am not sure what to call / query and cannot construct the script with my variables & field names. There are columns 'label' and 'parent ID' in the kb_category table. How could I make the query to get the list of categories based on their parent ID?
Thank you for your patience with my beginner brain. I believe getting help & script example for this could help a lot of other beginners too - so thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 04:45 AM
Hi @Community Alums ,
You can use the following reference qualifier script:
For variable 2: Subservice
(function() {
var parentService = current.variables.u_service.toString();
var kbCategory = new GetKBcategories().getCategories(parentService);
return "sys_idIN" + kbCategory.join(',');
})();
For variable 3: Topic
(function() {
var parentSubService = current.variables.u_subservice.toString();
var kbCategory = new GetKBcategories().getCategories(parentSubService);
return "sys_idIN" + kbCategory.join(',');
})();
And here is the sample script include GetKBcategories that you can use in your instance.
var GetKBcategories = Class.create();
GetKBcategories.prototype = {
initialize: function() {
},
getCategories: function(parent) {
var kbCategories = [];
var gr = new GlideRecord('kb_category');
gr.addQuery('parent_id', parent);
gr.query();
while (gr.next()) {
kbCategories.push(gr.sys_id.toString());
}
return kbCategories;
},
type: 'GetKBcategories'
};
This script include contains a function called getCategories, which takes the parent category as a parameter and returns an array of all child categories. The kbCategories array is populated by querying the kb_category table using GlideRecord and adding a condition to filter the records with the parent_id equals to the parent parameter.
In the reference qualifier script for variable 2 and variable 3, the GetKBcategories script include is called, passing the parent category ID as a parameter. The getCategories function returns an array of all child categories, which is then joined with commas and returned in the format required by the reference qualifier.
If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the ✅Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.
Thank you!
Ratnakar