The CreatorCon Call for Content is officially open! Get started here.

Function looks to return, but variable showing undefined

kristenankeny
Tera Guru

Hi all,

I'm hoping someone can see what I'm doing wrong here. I am attempting to iterated through the xml field on the task_ci table to print the proposed CI changes in an approval email for Change Requests. I am trying to build a comma separated string of the current table and all parents so that I can look for the fields in the dictionary to grab some information.

This is the result I'm getting in my testing:


Sending string back: cmdb_ci_linux_server,cmdb_ci_server,cmdb_ci_computer,cmdb_ci_hardware,cmdb_ci,cmdb

tables: undefined

This is the call to the function from the main script (which is returning "undefined"):

var lookforTables = getParentTables(tableNodeName,tableNodeName);

template.print('tables: ' + lookforTables);

This is the function that shows that it will return the entire string of table names:

function getParentTables(table,string){

var newString = string;

var thisTable = new GlideRecord('sys_db_object');

thisTable.addQuery('name',table);

thisTable.query();

if(thisTable.next()){

        if(thisTable.super_class){

                  newString += ',' + thisTable.super_class.name;

                  getParentTables(thisTable.super_class.name,newString);

        }

else{

        template.print('Sending string back: ' + newString);

        return newString;

}

}

}

I'm not sure what's wrong...

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Can we try like,



var lookforTables = getParentTables(tableNodeName);  


template.print('tables: ' + lookforTables);




function getParentTables(table){  


var tablehierarchy = new TableUtils(table);


return tablehierarchy.getTables();


}





View solution in original post

4 REPLIES 4

ruzzty06
Tera Expert

Hi,



I'm not sure if the line getParentTables(thisTable.super_class.name,newString); is gonna work.



but you can try using a try catch and log any error so you can see if your code is executing without errors.



You can also try a different approach.



var table='table name';


var tablenames=table;



var parent;


do {


      parent=getParent(table);


        if(parent)


        {


                  tablenames+=','+parent;


                  table=parent;


        }


}


while (parent);



function getParent(table) // not actual code, logic only


{


        return table.parent


}


Harish KM
Kilo Patron
Kilo Patron

can u try this


return 'sys_idIN' + newString;


Regards
Harish

Shishir Srivast
Mega Sage

Can we try like,



var lookforTables = getParentTables(tableNodeName);  


template.print('tables: ' + lookforTables);




function getParentTables(table){  


var tablehierarchy = new TableUtils(table);


return tablehierarchy.getTables();


}





Thanks! This worked like a charm!