Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto Poupulate the asset assigned to the user

Aruna13
Tera Contributor

Hi,

 

I have the catalog form where there are 2 reference fields.

1. user_to_be_released - reference of sys_user table

2. computer_name - reference of alm_hardware table

 

Now i need to auto populate the asset assigned to the user( "user_to_be_release field") in computer_name field  and below are the CS and script include which i have written. But i didnt get any values returned in computer_name field.

Can you please help me with this and is there any reference qualifier needs to added in computer_name field? Please give your suggestions, since i am new to scripting.

 

Client Script - written on computer_name and type is onChange

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

    // Client script for auto-populating computer name on change
    var ga = new GlideAjax('GetAssetName');
    ga.addParam('sysparm_name', 'getAssetName');
    ga.addParam('sysparm_req', newValue);
    ga.getXML(GetAssetNameParse);
 
    function GetAssetNameParse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('computer_name', answer);

    }

 

Script Include - client callable

var GetAssetName = Class.create();
GetAssetName.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getAssetName: function() {
        var req = this.getParameter('sysparm_user_to_be_released');
        var gr = new GlideRecord('alm_hardware');
        gr.addQuery('assigned_to', req);
        gr.addQuery('model_category','Computer');
        gr.query();
        if (gr.next()) {
            return gr.asset_tag; // returning the sys_id ensures that all fields in the reference are retrieved
        }
    },
    type: 'GetAssetName'

});

 

 

18 REPLIES 18

Aruna13
Tera Contributor

Thanks Jitendra,

 

Background script is now returning the value. So what should i do next, can you please guide me?

Jitendra Diwak1
Kilo Sage

Hi @Aruna13,

 

So now you can modify your query as below:

 

 var req = this.getParameter('sysparm_user_to_be_released'); 
var hardwaretoassignee = '';
var gr = new GlideRecord('alm_hardware');
        gr.addQuery('assigned_to', req);
        gr.addQuery('model_category','sys_id of model');// always user sys_id and try to avoid query by name
        gr.query();
        if (gr.next()) {
            hardwaretoassignee = gr.assest-tag;
        }
return hardwaretoassignee;

 

Please accept my solution if it works for you and thumps up.

 

Thanks

Jitendra

Please accept my solution if it works for and thumps up.

Aruna13
Tera Contributor

Hi @Jitendra Diwak1 ,

 

I modified script include as you suggessted. Dont I have to modify client script which i wrote, can you please check and guide me please.

 

Thanks

Hi @Aruna13,

 

Please replace this line in client script below:

ga.addParam('sysparm_user_to_be_released', newValue);

and make sure "computer_name' is reference and refer to alm_hardware.

 

Please accept my solution if it works for you and thumps up.

 

Thanks

Jitendra

Please accept my solution if it works for and thumps up.

Aruna13
Tera Contributor

@Jitendra Diwak1 

 

I have updated both my CS and Script include as below. But still the computer name is not getting auto populated. PFB screenshots. Please take a look and guide me. Do I have to write a reference qualifier for computer name field, i am not sure.

 

CS:

Aruna13_0-1717060073012.png

Script Include:

Aruna13_1-1717060173992.png

 

Computer_name variable:

Aruna13_2-1717060354750.png