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

Hi Doug,



After running your script, instead of returning " ", I am getting "undefined" being returned. I know this is close, I'll continue to have a look into what's going on.



Thanks for your input,


ok.. then what i would do is add a line to print the service name to verify it is finding the right service...



gs.print(service.name);   //instead of .name put in the field name for the service name.. or any other service field you can see


ryan86
Kilo Expert

The image below shows the element "managed_by", which I am trying in vain to retrieve from within a workflow.


image.PNG



If you scroll further, there are fields for name, and sys_id (which links back to the sys_users) table. Each script I have tried so far, has resulted in a variable equal to " " being returned?