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

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Replace the bewlow line in script include

     var req = this.getParameter('sysparm_user_to_be_released');

 

To

     var req = this.getParameter('sysparm_req');

-Anurag

Aruna13
Tera Contributor

hi @Anurag Tripathi ,

 

I tried updating, but it didnt work. 

Can you show the latest version of the script you are using?

-Anurag

Aruna13
Tera Contributor

@Anurag Tripathi below is my script include and catalog client script.

 

Script Include:

Aruna13_0-1717056920216.png

 

Client Script:

Aruna13_1-1717056965985.png

 

Thanks!