- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2022 08:37 AM
I have a form with 3 field: Tower, Name and fatherID
Example:
Record with sys_id = 001 : {Tower = Quality; Name = A; fatherID = 001}
Record with sys_id = 002 : {Tower = Quality; Name = AB; fatherID = 001}
Record with sys_id = 003 : {Tower = Quality; Name = ABB; fatherID = 002}
Record with sys_id = 004 : {Tower = Quality; Name = AC; fatherID = 001}
function getRaiz(torre) {
var recordRaiz = [];
var recraiz = {};
var gr = new GlideRecord('u_capability');
gr.addQuery('u_torre', '=', 'qualidade');
gr.addQuery('u_torre', '!=', '');
gr.orderBy('u_torre');
gr.query();
while (gr.next()) {
if (gr.getValue('u_parent_id') == gr.getValue('sys_id')) {
recraiz.name = gr.getValue('u_name');
gs.addInfoMessage('torre = ' + gr.getValue('u_name'));
recraiz.children = getChildren(gr.getValue('u_parent_id'));
recordRaiz.push(recraiz);
}
}
return recordRaiz;
}
function getChildren(id) {
var children = [];
var rec = {};
var name = '';
var gr = new GlideRecord('u_capability');
gr.addQuery('u_parent_id', id);
gr.addQuery('u_name', '!=', '');
gr.orderBy('u_name');
gr.query();
var total = gr.getRowCount();
if (total == 0)
return '';
while(gr.next()) {
id = gr.getValue('sys_id');
if (gr.getValue('u_parent_id') != gr.getValue('sys_id')){
gs.addInfoMessage(gr.getDisplayValue('u_parent_id') + " : " + gr.getValue('u_name'));
name = gr.getValue('u_name');
rec.name = name;
rec.children = getChildren(id);
gs.addInfoMessage(JSON.stringify(rec));
children.push(rec);
}
// gs.addInfoMessage('saiu');
}
return children;
}
getRaiz('qualidade');
================================
the issue is to return the sons to top father (inside the getRaiz() function)
Any idea on how to do this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2022 08:08 AM
I found the error in my code.
function getChildren(id) {
var children = [];
// var rec = {}; here is the wrong place
var name = '';
var gr = new GlideRecord('u_capability');
gr.addQuery('u_parent_id', id);
gr.addQuery('u_name', '!=', '');
gr.orderBy('u_name');
gr.query();
var total = gr.getRowCount();
if (total == 0)
return '';
while(gr.next()) {
var rec = {}; //here is the RIGHT place!
id = gr.getValue('sys_id');
if (gr.getValue('u_parent_id') != gr.getValue('sys_id')){
name = gr.getValue('u_name');
rec.name = name;
rec.children = getChildren(id);
children.push(rec);
}
}
return children;
}
Thanks for you help, Camacho!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2022 01:33 AM
Hi,
/*
Example:
Record with u_sysid = 001 : {Tower = Quality; Name = A; fatherID = 001}
Record with u_sysid = 002 : {Tower = Quality; Name = AB; fatherID = 001}
Record with u_sysid = 003 : {Tower = Quality; Name = ABB; fatherID = 002}
Record with u_sysid = 004 : {Tower = Quality; Name = AC; fatherID = 001}
*/
getRaiz('Quality');
function getRaiz(tower) {
var recordRaiz = [];
var gr = new GlideRecord('mytable');
gr.addQuery('tower', '=', tower);
gr.addQuery('tower', '!=', '');
gr.orderBy('tower');
gr.query();
while (gr.next()) {
var recraiz = {};
recraiz.u_sysid = gr.getValue('u_sysid');
recraiz.tower = gr.getValue('tower');
recraiz.name = gr.getValue('name');
recraiz.fatherid = gr.getValue('fatherid');
if (gr.getValue('fatherid') == gr.getValue('u_sysid')) {
recraiz.children = getChildren(gr.getValue('tower'), gr.getValue('fatherid'), recraiz);
recordRaiz.push(recraiz);
}
}
gs.addInfoMessage(JSON.stringify(recordRaiz));
}
function getChildren(tower, id, recraiz) {
var children = [];
var gr = new GlideRecord('mytable');
gr.addQuery('tower', tower);
gr.addQuery('fatherid', id);
gr.addQuery('name', '!=', '');
gr.orderBy('name');
gr.query();
var total = gr.getRowCount();
if (total == 0)
return '';
while(gr.next()) {
var u_sysid = gr.getValue('u_sysid');
var tower = gr.getValue('tower');
var name = gr.getValue('name');
var fatherid = gr.getValue('fatherid');
var rec = {};
rec.u_sysid = u_sysid;
rec.tower = tower;
rec.name = name;
rec.fatherid = fatherid;
if (recraiz.u_sysid != rec.u_sysid) {
children.push(rec);
}
}
return children;
}
I did some refactoring and now I believe you're good to go.
Please mark my response as Correct or Helpful, if you find it appropriate.
________________________
Carlos Camacho
https://www.linkedin.com/in/camachojunior
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2022 03:33 AM
The function GetChildren() should be recursive to get all tree childrens, cause some childrens can have childrens.
Another example: My familly.
My father has 3 childrens = {Fabio, Tati, Rod}
Fabio = {}
Tati = {Valentina}
Valentina = {Pedro}
Pedro = {}
Rod = {Murilo, Bernardo}
Murilo = {}
Bernardo = {}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2022 04:24 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2022 04:57 AM
Here is the record TOP.
The filed Torre (or tower) is the head.
This record id has the same value inside the "ID Pai" field.
"ID Pai" field is a referece field. (he show a display value, but inside it, has the id value)
Now the ID Pai field has the same value of the header, but the name changed. So this is a children record.
Now the ID Pai field changed to the children. So this record is a children from the children record.