Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Using gs.getUserName to get the Requested For

desertmoxie1
Tera Expert

We built a business rule to post RITM variable info as an sc_task worknote so technicians have the information at their fingertips without going back to RITM.  However, I'm trying to get the Requested For from the Request/RITM to display, but it's not working.

Everything in the below code works except for the bolded //get Username from Requested for.  Would appreciate input, thanks!

(function executeRule(current, previous /*null when async*/) {

// get Username from Requested For
var username = gs.getUserName(current.requested_for);

//Get Variables and loop through
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.getValue('request_item'));
var workNote = "";
set.load();
var vs = set.getFlatQuestions();
gs.log('Size: ' + vs.size());
for (var i = 0; i < vs.size(); i++) {
if (vs.get(i).getDisplayValue() != '') { // Only get variables that were filled out
workNote = workNote + vs.get(i).getLabel() + ": " + vs.get(i).getDisplayValue() + "\n";
}
}

current.work_notes = "Requested for user name: " + username + "\n" + workNote + "\n";
current.update();


})(current, previous);

1 ACCEPTED SOLUTION

desertmoxie1
Tera Expert

Thanks, we had to use the following with dot-walk for request.requested_for in order to work.

var username = current.request.requested_for.getDisplayValue();

View solution in original post

7 REPLIES 7

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Desertmoxie,

Seems like the question is already resolved but just as a note, gs.getUserName() does not take any argument.

FYR,

https://developer.servicenow.com/dev.do#!/reference/api/rome/server_legacy/c_GlideSystemAPI#r_GS-get...

 

 

DrewW
Mega Sage

As others have pointed out there are some easier ways to get the user name.  Keep in mind that there are now two different "Requested for" fields.  One is on the Request that the Requested Item is attached to and this is the oldest one and the one the system is useally setup to use.  Several versions ago when they introduced the "Requested for" variable type they added another "Requested for" field to the Requested item table.  These two different fields are set based on if you have a "Requested for" variable on your catalog item.

https://docs.servicenow.com/bundle/rome-servicenow-platform/page/product/service-catalog-management/reference/r_VariableTypes.html#d280642e1223

So depending on how your catalog item is setup you can use either of the two below to get the user_name field or the display value for the user

user_name field
var username = current.requested_for.user_name.toString();
var username = current.request.requested_for.user_name.toString();

For display value
var username = current.requested_for.getDisplayValue();
var username = current.request.requested_for.getDisplayValue();

 

Thanks for all the help!!!