Get Element value - scripting

ryan86
Kilo Expert

Hi all

 

(I'm new to ServiceNow)

 

I have got a form that users complete to request a new account be created. I capture an business service chosen in the form, and then need to find the manager of that service.

 

At present, the following code saves the service chosen into a variable, which I can add to the description field.  

 

var service = current.variable_pool.srv_service; (Gives me the sys_id)

var servname = service.getDisplayValue(); (Gives me the value of the "name" column for the above sys_id)


It has become apparent, that I now need to get the manager for the service. I have tried all below, with no success.


var man = service.managed_by;

var man = service.getElement('managed_by');

var man = service..managed_by.name;


I have also tried creating a glideRecord and querying the table ('cmdb_ci_service'), but again, no success. There is a column named "Managed By", in the 'cmdb_ci_service' table.


I'm sure it's something simple I'm missing?


Thanks,

 

1 ACCEPTED SOLUTION

Ryan thanks from that you can see that this field is on the cmdb_ci_service table... which is critical to querying to get it... so your script should be...



var service = new GlideRecord('cmdb_ci_service');


service.addQuery('sys_id', 'current.variable_pool.srv_service');


service.query()


while(service. next()){


  var servname = service.managed_by.getDisplayValue();


}



i would run it as a background script by putting a SID for one of your services in where i have sid below.



var sid='sid';


var service = new GlideRecord('cmdb_ci');


service.addQuery('sys_id', sid);


service.query()


while(service. next()){


  var servname = service.getDisplayValue();


  gs.print(servname);


}



once you can retrieve the manager from a background script like this you are ready to put it in a workflow and replace the hardcoded sid with a variable name.. just take otu the gs.print command.


View solution in original post

17 REPLIES 17

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Ryan,



Try this script:


var service = current.variable_pool.srv_service;


var inc = new GlideRecord('sys_user');


inc.addQuery('sys_id', 'service');


inc.query()


while(inc. next())


{


var mgr = inc.manager;


gs.addInfoMessage(mgr);


var manage = inc.manager; //I am assuming you have manag field on the form


inc.update(); //dont use this if you write business rule before update


}



Thanks,


Pradeep Sharma



Hi Pradeep,



I'm looking to take the service sys_id from the form, and then find the manager for it. There is not a "manager" field on the form, as if there was, I wouldn't need to look it up?



Also, why would I need to query the 'sys_user' table, with a sys_id of a Business Service?



Thanks,


Hi Ryan,



I was confused with the question


Here you go


var service = current.variables.srv_service;


var inc = new GlideRecord('cmdb_ci_service');


inc.addQuery('sys_id', service);


inc.query();


while(inc.next())


{


var manag = inc.managed_by.getDisplayValue();


gs.addInfoMessage(manag);


}



Thanks


Pradeep Sharma


Subhajit1
Giga Guru

For more requirements like this......kindly refer to this Wiki link:-


Getting a User Object - ServiceNow Wiki