Using a Dynamic Field Name Reference in a Glide Record in Scripting

jmiskey
Kilo Sage

I am trying to create a Scheduled Job to send out email notifications.  It is looking at specific fields in specific tables, and returning a list of Inactive Users listed in that field (so all these fields would be references to the sys_user table).

So, we are creating a "Control Table", that lists all the Tables & Fields we want to search.  Here is what one of those records looks like (searching the "Manager" field in the "Group" table):

find_real_file.png

So the "Field Name" field is just a reference to the "Dictionary Entry" table, and once that field is populated, I have a Client Script that populate the "Table Name" field for that record. 

That is all working fine.  The issue I have is with the script I am beginning to build for my Scheduled Job.  Here is what I have so far:

//run through all active records in u_terminated_approver_notifications table
var gr1 = new GlideRecord('u_terminated_approver_notifications');
gr1.addQuery('u_inactive', 'False');
gr1.query();

while(gr1.next()){
	var users = '';

	//capture field values
	var tname = gr1.u_table_name;
	var fname = gr1.u_field_name.element;
	var act = gr1.u_active_inactive_field_name;
	
        //build encoded query
	var encqry = fname + '!=NULL^' + fname + '.active=false';
	if(act='active'){
		encqry += '^active=true'
	}else{
		if(act='u_inactive'){
		encqry += '^u_inactive=false'
		}
	};

	//create new glide record for look for terminated people in tables
	var gr2 = new GlideRecord(tname);
	gr2.addEncodedQuery(encqry);
	gr2.query();

	while(gr2.next()){
		var user = gr2.manager.getDisplayValue();
		users += user + ', ';
	}

	//return string
        gs.print('Table:  '  + tname + ' Field:  ' + fname + ' Users: ' + users);
}

So, the first Glide Record is simply looping through my Control table, and pulling the values from each active record, and then for each one, running another Glide Record on that table to look for terminated users in the designated field, and write them back to the "users" string, and return the results to a string (currently just doing this in a Background Script while testing my code out).

The issue is with this line here:

var user = gr2.manager.getDisplayValue();

It works for the specific example that I did a screen print for, because I am returning the value from the "manager" field in the Group table, and I hard-coded "manager" in my code.  But what I really want is to not hard-code this part (which would defeat the purpose), and to instead use the name of the field (the fname variable).

But JavaScript and GlideRecords do not like the use of variables in place of the field name, that is, the following will not work:

var user = gr2.fname.getDisplayValue();

But that is the functionality I want to happen.  I want to return the value of the field stored by in the dynamic fname variable.

Does anyone how how I can do this in a Glide Record?  How I can reference the name of the field I want to return dynamically instead of hard-coding it?

Thanks

 

 

 

 

1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

var user = gr2.getDisplayValue(fname);

or if you want the value, you can use

var user = gr2.getValue(fname);

View solution in original post

2 REPLIES 2

Jon Barnes
Kilo Sage

var user = gr2.getDisplayValue(fname);

or if you want the value, you can use

var user = gr2.getValue(fname);

Thank you very much!  I also discovered that this works as well:

var user = gr2.getElement(fname).getDisplayValue();