How to create a tree view?

Rodrigo Lyra
Tera Expert

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?

1 ACCEPTED SOLUTION

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!

View solution in original post

6 REPLIES 6

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!

Hi, 

I'm happy to know that my answers were helpful. Please mark it as Helpful and/or Correct.