How to get value from g:ui_reference and pass to script include parameter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 03:14 AM
Hello Team,
I have a requirement to show all level child categories of selected knowledge base from the UI page.
There is a script include to show all child categories. But now I need to pass the Knowledge base selected in the UI page to load the child list.
Could anyone tell how to pass g:ui_reference value to script include parameter which is called in another g:ui_reference?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 04:03 AM
Hi @Dhanakoddi2,
Hope you are doing well.
As per my understanding, you want to get the value of the "knowledge Base" selected from "UI Page" which come from <g:ui_reference> type field so that you can show all the child list into another <g:ui_reference> type field. in short, second Ui reference should be dependent on first Ui reference and for this you don't need to include any Script Include as well. If I am understanding your concern correctly, Here is the solution proposed down below:-
Proposed Solution
As a solution, you just need to follow 2 steps to get the solution of your query:-
- Add "onchange="getChildList()" attribute in the first <g:ui_reference> (of Knowledge Base Selected Field).
- Add below script in Client Script Section of the UI Page.
// Making second UI Reference Field Dependent on first UI Reference Field
function getChildList(){
var ffn = gel('first_field_name').value; // Replace "first_field_name" with the name of the field to get the value.
var userLookup = gel('lookup.second_field_name'); // Replace "second_field_name" with the name of the field.
// From here you can start making the GlideRecord on the table from where you want to get the list of all the child as per the selected knowledge base.
var child = [];
var gr = new GlideRecord('table_name');
gr.addQuery(); // add condition here like "sys_id", ffn;
gr.query();
while(gr.next()){
child.push();
}
userLookup.setAttribute('onclick', "mousePositionSave(event); reflistOpen('second_field_name', 'not', 'second_field_table_name', '', 'false', 'QUERY:active=true', 'sys_idIN" + child + "', '')");
}
If you find this helpful, Please don't forget to mark my solution and reply as helpful and accepted.
Thanks 🙂
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 06:31 AM - edited 03-29-2024 06:40 AM
Hello Akash,
Thanks for the reply. I have updated the script with your code it working for only first level. My requirement is to show all level child list.
Example:
Knowledge Base : ServiceNow
Level 1 Child Category : A
Level 2 Child Category : B (Parent is 'A')
Level 3 Child Category : C (Parent is 'B')
Now we need to Show all 'A','B','C' Categories when 'ServiceNow' is selected in Knowledge Base.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 06:48 AM
Hi @Dhanakoddi2,
Glad to see that you are following my code and proposed solution.
As per you are getting nothing/showing everything, can you pls elaborate this more in detail like if you select "ServiceNow" in first UI Reference filed, what all values you want to see or what values are getting visible in second UI Reference field as of now.
With this, I hope you didn't forget to add query in gr.addQuery() line in Client Script which will fetch the records that are specific to "ServiceNow".
Answer to your question - can we do Glide Record on client script?
Yes, we can make "GlideRecord and GlideAjax" call whatever we want to do, we can do in Client Script and Processing Script of UI Pages.
If you find this information/knowledge/solution helpful, Please don't forget to mark my solution and reply as helpful and accepted.
Thanks 🙂
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 07:59 AM
Hello Aakash,
Yes your code is working fine for first level categories. I have corrected my previous comment.
But for my requirement, it needs to show all level categories.
For example,
Knowledge Base : ServiceNow
Level 1 Child Category : A
Level 2 Child Category : B (Parent is 'A')
Level 3 Child Category : C (Parent is 'B')
Level 3 Child Category : D (Parent is 'B')
Now we need to Show all 'A','B','C','D' Categories when 'ServiceNow' is selected in Knowledge Base.
Could you please help on this how we can achieve?