Convert sys_id to displayed value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2020 04:23 PM
Hi All, I'm very new to service-now development and I'm currently working on developing a form in catalog item where someone can request to have a server rebooted. I have a server field on the form that when updated with a servername I would like to supply additional information from its ci record in the cmdb to other variables I've created on the form. Below is the approach I'm taking and I am getting all the fields to populate, however they're mostly coming back as the sys_id string. Am I targeting the right fields in this example, or should I take a different approach to populate the displayed information? I appreciate any help on this!!
function onChange(control, oldValue, newValue, isLoading) {
g_form.setValue('ref_data_center_location', '');
g_form.setValue('slt_product', '');
g_form.setValue('slt_environment', '');
g_form.setValue('slt_solution', '');
g_form.setValue('slt_governance_area', '');
g_form.setValue('wlt_business_function', '');
g_form.setValue('slt_defined_maintenance_schedlule', '');
g_form.setValue('mlt_more_information_user_defined', '');
g_form.setValue('slt_physical_server', '');
if (g_form.getValue('ref_server_name') != ''){
g_form.setValue('ref_data_center_location', g_form.getReference('ref_server_name').u_datacenter); **I get display info**
g_form.setValue('slt_product', g_form.getReference('ref_server_name').u_product);
g_form.setValue('slt_environment', g_form.getReference('ref_server_name').u_used_for); **I get display info**
g_form.setValue('slt_solution', g_form.getReference('ref_server_name').u_solution);
g_form.setValue('slt_governance_area', g_form.getReference('ref_server_name').u_governance_area);
g_form.setValue('wlt_business_function', g_form.getReference('ref_server_name').u_business_function);
g_form.setValue('slt_defined_maintenance_schedlule', g_form.getReference('ref_server_name').maintenance_schedule);
g_form.setValue('mlt_more_information_user_defined', g_form.getReference('ref_server_name').u_ehi_description);
g_form.setValue('slt_physical_server', g_form.getReference('ref_server_name').virtual); **I get display info**
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2020 05:23 PM
With client-side scripting, you are somewhat limited in the data that you can pull back. This would be an instance where GlideAjax would be useful.
You would need to create a client callable script include that would perform your data lookup on the server side to return the display values. It would look something like this (to get the values for the ones you have flagged as get display value)
The name should be serverData.
Client Callable should be checked.
The script would be the following:
var serverData = Class.create();
serverData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
type: 'serverData',
getData: function() {
var retStr = 'def';
var gr = new GlideRecord('cmdb_ci_server');
gr.addQuery('sys_id', this.getParameter('sysparm_id'));
gr.query();
while(gr.next()) {
retStr += gr.getDisplayValue('u_product') + ',';
retStr += gr.getDisplayValue('u_solution') + ',';
retStr += gr.getDisplayValue('u_governance_area') + ',';
retStr += gr.getDisplayValue('u_business_function') + ',';
retStr += gr.getDisplayValue('maintenance_schedule') + ',';
retStr += gr.getDisplayValue('u_ehi_description');
}
return retStr;
}
});
The above assumes you are looking at a cmdb_ci_server record with some custom fields. If this is not correct, you need to change the table name for the GlideRecord call to be the table you reference in ref_server_name.
You would then call this client side with something like this in an onchange for the ref_server_name field:
var ga = new GlideAjax('serverData');
ga.addParam('sysparm_name', 'getData');
ga.addParam('sysparm_id', g_form.getValue('ref_server_name'));
ga.getXML(processResponse);
function processResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var answers = answer.split(',');
g_form.setValue('slt_product', answers[0]);
g_form.setValue('slt_solution'', answers[1]);
g_form.setValue('slt_governance_area', answers[2]);
g_form.setValue('wlt_business_function', answers[3]);
g_form.setValue('slt_defined_maintenance_schedlule', answers[4]);
g_form.setValue('mlt_more_information_user_defined, answers[5]);
}
The above may not be exactly what you need, but it should come pretty close and should give you a good idea of how to accomplish what you are looking to do.
If this was helpful, or the correct answer, please be kind and click appropriately!
Michael D. Jones
Proud member of the GlideFast Consulting Team!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2020 09:55 PM
Hi,
You can use
1.gr.getDisplayValue('ref_server_name'); OR
2.gr.ref_server_name.getDisplayValue();
If my answer helps you please mark as correct/helpful
Regards,
Monali