Populate asset tag from cmdb_ci_computer on incident

Andrew_TND
Mega Sage
Mega Sage

Hello,

Wondering if anyone can help me...

Every user has an assigned computer within the table cmdb_ci_computer and what I want to achieve is an onChange client script which when the caller changes it looks at the table and populates the asset tag field on the incident form.

I've got the beginnings of my script below but cant seem to get it to work. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var gr = new GlideRecord("cmdb_ci_computer");
	gr.addQuery("assigned_to", caller_id);
	gr.query();
   if (gr.next()){
	var sysid = gr.getValue('sys_id');
	g_form.setValue("u_asset_tag", sysid);
   }
}


 

4 REPLIES 4

jMarshal
Mega Sage
Mega Sage

I assume your "u_asset_tag" field is string? -- or is it a reference to cmdb_ci_computer?

...one thing you'll want to do is change the glide query to be "gr.addquery("assigned_to", current.caller_id);"...and if the asset tag field is a string, you'll want to return/set the "name" value to asset tag, not sysID of the record found by the glide.

Hi, I cant use current as its a client script but no, the asset tag field is referenced to the cmdb_ci_computer table

Sorry I misread. You'll need to rework this a bit...client scripts do not have access to glide, you need to use glide ajax instead -- GlideAjax | ServiceNow Developers

Okay, I've done the glideAjax, but still cant seem to get it to work if you have any suggestions...

//script includes
var setAssetTagTag = Class.create();
setAssetTagTag.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    populateAssetTag: function() {
        var gr = new GlideRecord('cmdb_ci_computer');
        gr.addQuery('sys_id', this.getParameter('sysparm_cmdb_ci_computer'));
        gr.addQuery('assigned_to', current.getDisplayValue("caller_id"));
        gr.query();
        if (gr.next()) {
            return gr.getValue("sys_id");
        }
    },

    type: 'setAssetTag'
});
//client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('setAssetTag');
    ga.addParam('sysparm_name', 'populateAssetTag');
    ga.addParam('sysparm_cmdb_ci_computer', g_form.getValue('cmdb_ci_computer'));
    ga.getXML(getResponse);

    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_asset_tag',answer);
    }


}