glide script function with multiple variables

brkelly
Kilo Expert

I am calling a the function 'gettables' to identify certain fields in the dictionary, and their associated tables.   Then I call the 'recforuser' function to identify records for that user on the tables.

The challenge I am having is that I don't know java or glide scripting.     I pass the variable 'field' to the second function, but I don't know how to use that to get the gr field I need.

The line I am having trouble with is:

gr.owned_by.name

I want it to be dynamic passing the variable 'field'.   I think it should look something like ---   gr. && 'field' && .name ??

The full script is below.

____________________________________

gettables("table_name");

// ~~~~~~~~~ GET table fields for evaluation

function gettables(sys_dictionary) {

  var gr = new GlideRecord('sys_dictionary');

  var cnguser = 'Abel Tuter'

  gr.query();

  while (gr.next()){

  if (gr.internal_type=='reference'&&gr.reference=='sys_user'&gr.u_life_cycle!=""){

  gs.print('Table:' + gr.name + " Type:" + gr.internal_type + " Reference:" + gr.reference + " Lifecycle:" + gr.u_life_cycle.name + " Column Name:" + gr.element );

  var table = gr.name

  var field = gr.element

  //~~~~~~ run function "records for user" recforusr

  recforusr(table,field,cnguser);

  }

}

}

// HARD CODED ALM_ASSET and ABEL TUTER as well as OWNED By FIELD

// ~~~~~~~~~ GET Records for user Function

function recforusr(table,field,cnguser) {

  var gr = new GlideRecord(table);

  gr.query();

  while (gr.next()){

  if (gr.owned_by.name == cnguser){

  gs.print('Field:' + field   + " Name:" + gr.name + " Display Name:" + gr.display_name + " Owned By:" + gr.owned_by.name);

  }

}

}

1 ACCEPTED SOLUTION

Steve McCarty
Mega Guru

Generally you don't want to do a GlideRecord query to return every record on the table and then loop through them.   When starting out that may not be too bad, but very quickly that is going to eat up all your resources.   You want to use the .addQuery() function to limit your results before trying to loop through them.   This is a good page for GlideRecord info: Using GlideRecord to Query Tables - ServiceNow Wiki .   As for your specific question you can try something like this:



gettables("table_name");


// ~~~~~~~~~ GET table fields for evaluation


function gettables(sys_dictionary) {


  var gr = new GlideRecord('sys_dictionary');


  var cnguser = 'Abel Tuter'


  gr.addQuery('internal_type','reference');


  gr.addQuery('reference','sys_user');


  gr.addQuery('u_life_cycle','!=','');


  gr.query();


  while (gr.next()){


  gs.print('Table:' + gr.name + " Type:" + gr.internal_type + " Reference:" + gr.reference + " Lifecycle:" + gr.u_life_cycle.name + " Column Name:" + gr.element );


  var table = gr.name


  var field = gr.element


  //~~~~~~ run function "records for user" recforusr


  recforusr(table,field,cnguser);


  }


}



// ~~~~~~~~~ GET Records for user Function


function recforusr(table,field,cnguser) {


  var gr = new GlideRecord(table);


  gr.addQuery(field + '.name', cnguser);


  gr.query();


  while (gr.next()){


  gs.print('Field:' + field   + " Name:" + gr.name + " Display Name:" + gr.display_name + " Owned By:" + gr.owned_by.name);


}


}



View solution in original post

3 REPLIES 3

Steve McCarty
Mega Guru

Generally you don't want to do a GlideRecord query to return every record on the table and then loop through them.   When starting out that may not be too bad, but very quickly that is going to eat up all your resources.   You want to use the .addQuery() function to limit your results before trying to loop through them.   This is a good page for GlideRecord info: Using GlideRecord to Query Tables - ServiceNow Wiki .   As for your specific question you can try something like this:



gettables("table_name");


// ~~~~~~~~~ GET table fields for evaluation


function gettables(sys_dictionary) {


  var gr = new GlideRecord('sys_dictionary');


  var cnguser = 'Abel Tuter'


  gr.addQuery('internal_type','reference');


  gr.addQuery('reference','sys_user');


  gr.addQuery('u_life_cycle','!=','');


  gr.query();


  while (gr.next()){


  gs.print('Table:' + gr.name + " Type:" + gr.internal_type + " Reference:" + gr.reference + " Lifecycle:" + gr.u_life_cycle.name + " Column Name:" + gr.element );


  var table = gr.name


  var field = gr.element


  //~~~~~~ run function "records for user" recforusr


  recforusr(table,field,cnguser);


  }


}



// ~~~~~~~~~ GET Records for user Function


function recforusr(table,field,cnguser) {


  var gr = new GlideRecord(table);


  gr.addQuery(field + '.name', cnguser);


  gr.query();


  while (gr.next()){


  gs.print('Field:' + field   + " Name:" + gr.name + " Display Name:" + gr.display_name + " Owned By:" + gr.owned_by.name);


}


}



Wow much faster!   Excellent!


brkelly
Kilo Expert

That's great Stephen   I will give that a go, I had fixed my immediate issue using gr.getValue but looking at yours I think it is much better.   I will work to understand it more.   Thanks!   -- this code is intended to help people build lifecycles around fields like the user fields in asset tables.   It will allow a 'lifecycle'/request/workflow to execute if a user changes jobs etc..