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

Hi Chuck



I ran your code and I go this in the log



Sorry, could not find a record in u_rfa_environments with u_environments set to: 56bdda374fc33200505be9628110c73a



What do you think is happening?


Hi Alfredo,



Ah, that's a good sign. We've got a sys_id.



Next, go to the application menu filter and type:



u_rfa_environments.list



That will bring up a list of all your records. Use the filter to see if you can find a record where u_environments.



Environments | is | 56bdda374fc33200505be9628110c73a



Q: Is Environments a reference field? A string? A list field? What type is it? The plural indicates that it may be a list field, but I want to know for sure.



Thank you


Thank you Chuck for your help



Environments is a string field, see below a screenshot of the three fields of my table u_environments.



A little background of my project. I'm creating a workflow to provide access to several environments. There are some that don't need approval and there are some that do. So after the user submits the request, I want to check if the environment has an approver, if it does, I want the approval task assign to that approver.




Screen Shot 2017-06-07 at 12.09.11 PM.png


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);


}


Thank you Chuck, that script is working perfectly