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

I have added line 8 in script include only to query the asset which has model category as computer 

Model category is reference field, so change that to 

gr.addQuery('model_category.name', 'Computer');

 

Run the script include in a background script once with a hard coded value of the parameter you are passing to make sure that it returns the value you are expecting it to. 

All alerts and info message to debug.

-Anurag

Aruna13
Tera Contributor

When I run the background script, it works , but computer name field is not populating any data

 

Sorry I didn't get you.

What is not working in background script.

 

-Anurag

Jitendra Diwak1
Kilo Sage

Hi @Aruna13,

 

Best practice is to run the same code in background script so you can retrieve the exact result as per requirement. Please try this code in background script below:

 

 var gr = new GlideRecord('alm_hardware');
        gr.addQuery('assigned_to', 'sys id of user which is assigned to any user');
        gr.addQuery('model_category','pass the sysid of model category');// always user sys_id and try to avoid query by name
        gr.query();
        if (gr.next()) {
            gs.info('Assest tag '+ gr.asset_tag); // returning the sys_id ensures that all fields in the reference are retrieved
        }

 

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.