Script in workflow to set field values

Michael Bachme1
Kilo Guru

I have a Service Catalog item that asks the end user for a secondary email address.

I want a workflow that takes the user's value and inserts it into a field on the user's record. The script below is what I have, but it doesn't populate that user's record. I've tried the glide record functionality and simply copying the variable to the field (hence the commented out portions). What am I missing?

/*var user = new GlideRecord('sys_user');

user.addQuery('name', current.request.requested_for);

user.query();*/

current.request.requested_for.u_email = current.variables.secondary_email;

/*user.u_email = current.variables.secondary_email;*/

gs.log(current.request.requested_for.getDisplayValue() + ' ' + current.request.requested_for.u_email);

//user.update();

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Michael,



You were close with the GlideRecord query. I would use something like this though:



var user = new GlideRecord('sys_user');


if (user.get(current.request.requested_for)) {


  user.u_email = current.variables.secondary_email;


  user.update();


}


View solution in original post

2 REPLIES 2

GregJWillis
ServiceNow Employee
ServiceNow Employee

I believe the problem is that 'name' is a text field on the user's record (calculated from First, Middle and Last) and your searching the 'current.request.requested_for' value which is a Sys_ID to the referenced user record.



Try:


user.addQuery('sys_id', current.request.requested_for.sys_id);


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Michael,



You were close with the GlideRecord query. I would use something like this though:



var user = new GlideRecord('sys_user');


if (user.get(current.request.requested_for)) {


  user.u_email = current.variables.secondary_email;


  user.update();


}