Script to assign approver from a table field

Fredo1
Giga Expert

I have the following script to assign the approver from a table(u_rfa_environments) field(u_approver)

Table = u_rfa_environments

Fied1 = u_environments

Field2 = u_approver

Here is the script

answer = [];  

var app1 = current.variables.c_access_level;

var gr = new GlideRecord('u_rfa_environments');

gr.addQuery('u_environments', app1);

gr.query();

if (gr.next()) {  

            answer.push(gr.u_approver.toString());  

}

1 ACCEPTED SOLUTION

I think we've found the problem then. You're variable contains a sys_id which indicates that it's a reference field. Your Environments field is a string, which is likely to contain other values. There's a misalignment of data types.



I'm going to make an assumption here and figure that someone saw the display value in the variable (what gets shown to represent the record pointed to by the sys_id) and thought that is what they can use to get the proper record from the RFA Environments table. If that's the case, then try this (simple change to second line). It gets the display value instead of the sys_id from that variable. It's a bit like trying to find a record in table X that has a string "Chuck Tomasi" when all I have is a reference to my sys_user record (a sys_id.) If the getDisplayValue() returns "Chuck Tomasi", then I'm better off finding that record in table X.



Give it a try and let me know.



answer = [];


var app1 = current.variables.c_access_level.getDisplayValue();


var gr = new GlideRecord('u_rfa_environments');


if (gr.get('u_environments', app1)) {


        answer.push(gr.getValue('u_approver'));


} else {


      gs.error('Sorry, could not find a record in u_rfa_environments with u_environments set to: ' + app1);


}


View solution in original post

9 REPLIES 9

Chuck Tomasi
Tera Patron

What is the issue here Alfredo?



It appears you are trying to get a record based on the c_access_level variable. Are you sure it has a value? You could try forcing the type to a string and even shorten up the query a bit.



answer = [];


var app1 = current.variables.c_access_level.toString();


var gr = new GlideRecord('u_rfa_environments');


if (gr.get('u_environments', app1)) {


        answer.push(gr.getValue('u_approver'));


} else {


      gs.error('Sorry, couldn't find a record in u_rfa_environments with u_environments set to: ' + app1);


}


I am trying to assign the approval from that field (u_approver) and my script is not doing that.



I tried your code and I am getting errors on syntax



Screen Shot 2017-06-06 at 4.07.40 PM.png



Thank you Chuck


nishailame
ServiceNow Employee
ServiceNow Employee

Alfredo, you are getting syntax error because of quotes in couldn't.



answer = [];


var app1 = current.variables.c_access_level.toString();


var gr = new GlideRecord('u_rfa_environments');


if (gr.get('u_environments', app1)) {


        answer.push(gr.getValue('u_approver'));


} else {


      gs.error('Sorry, could not find a record in u_rfa_environments with u_environments set to: ' + app1);


}



At each level, try to print out the values of variable.


Sorry - two issues. I should not have used "shouldn't", the quote is messing up the string. Change that to:



gs.error('Sorry. Could not find a recor din u_rfa_environments with u_environments set to: ' + app1);



Also, you are missing the closing curly brace ( } ) on line 8. And of course, I forgot my disclaimer that the code was untested.



Updated script:



answer = [];


var app1 = current.variables.c_access_level.toString();


var gr = new GlideRecord('u_rfa_environments');


if (gr.get('u_environments', app1)) {


        answer.push(gr.getValue('u_approver'));


} else {


      gs.error('Sorry, could not find a record in u_rfa_environments with u_environments set to: ' + app1);


}