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

Ryan is the managed by field a reference field for sys   user?! if so the .getDisplayValue() method should work...



if not whichever table it is on you will need to either query to get the name .. or set the display value on that table.


solutioningnow
Giga Guru

Hi,



Can you try below script:



var service = current.variables.srv_service;


var gr = new GlideRecord('cmdb_ci_service');


gr.get(service);


var man = gr.managed_by.getDisplayValue();



Please mark answer as correct/helpful, if it was really helpful 🙂



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


Hi solutioner_now,



Your script returns an empty variable, as in " ";



Thanks


Hi Ryan,



Can you just confirm the field names. I tired similar script for different field in demo005 and its giving proper value, I'm pasting the reference script:



var dept = current.variables.depart;


var gr = new GlideRecord('cmn_department');


gr.get(dept);


gs.addInfoMessage(gr.dept_head.getDisplayValue())




Please mark answer as correct/helpful, if it was really helpful 🙂



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


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.