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.

Client script help

servicenow_devo
Tera Expert

Hi Team,

I have requirement for service catalog. I would need to populate list of assigned assets to the user.

It should be a onchange client script and if user has any assets assigned should populate and show up the name of assets.

 

Thanks in advance

Thankyou
1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hello @servicenow_devo 

 

I have achieved this by using variable type as a 'HTML'.

Can you check if this works for you....!!

 

Step 1 : Created two variables 

A] Name : User (user)

     Type   : Reference

     Table  : sys_user 

 

B] Name : My Assets (my_assets)

     Type   : HTML

     Read only : true

 

VishalBirajdar_0-1696339035146.png

 

Portal view : 

 

VishalBirajdar_1-1696339080863.png

 

 

Step 2 : Write Client script and script include as below 

 

Script include :

Name - assetDetailUtils

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


    getAssetDetails: function() {
        
		/*1. Get user from client script */
        var user = this.getParameter('sysparm_user');

        /*2. Declare one array to send results */
        var assetarray = [];
        
		/*3. Glide record on Hardware table - you can use your table */
        var grHardware = new GlideRecord('alm_hardware');
        grHardware.addQuery('assigned_to', user);  //query with assigned to is user
        grHardware.query();

        while (grHardware.next()) {
           /* 4. Get detalis in JSON */
            var assetDetails = {};
            assetDetails.asset_tag = grHardware.getValue('asset_tag').toString();
            assetDetails.serial_num = grHardware.getValue('serial_number').toString();
            

			/* 5. Push the JSON in Array */
            assetarray.push(JSON.stringify(assetDetails));
        }

        /* Return Array */
        return assetarray.join('|');

    },

    type: 'assetDetailUtils'
});

 

OnChange client script on 'User' variable :

 

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


    var user = g_form.getValue('user');
    var values;
    var ga = new GlideAjax('assetDetailUtils');   //script include
    ga.addParam('sysparm_name', 'getAssetDetails');  //script include function
    ga.addParam('sysparm_user', user);

    ga.getXMLAnswer(callBack);

    function callBack(answer) {

        var result = answer.split('|');
     
        for (var i = 0; i < result.length; i++) {

            if (values) {
                values = values + '<p>' + 'Asset Tag : ' +  JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+   JSON.parse(result[i]).serial_num +'</p>';
            } else {
                values = '<p>' + 'Asset Tag : ' +  JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+   JSON.parse(result[i]).serial_num +'</p>';
            }
        }
        g_form.setValue('my_assets', values);
    }

}

 

 

Output : 

 

VishalBirajdar_3-1696339809064.png

 

 

 

VishalBirajdar_2-1696339736191.png

 

 

Hope this works...!!!

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

7 REPLIES 7

Hi @Manoj89 

It's still not giving me required output. If you can help me with script Include and code it will be helpful.

Thankyou

Vishal Birajdar
Giga Sage

Hi @servicenow_devo 

 

Rich text label is just a label to provide some message or information.

You cant change it dynamically.

 

If you want to display asset assigned to user then use Multi Row variable set.

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Vishal Birajdar
Giga Sage

Hello @servicenow_devo 

 

I have achieved this by using variable type as a 'HTML'.

Can you check if this works for you....!!

 

Step 1 : Created two variables 

A] Name : User (user)

     Type   : Reference

     Table  : sys_user 

 

B] Name : My Assets (my_assets)

     Type   : HTML

     Read only : true

 

VishalBirajdar_0-1696339035146.png

 

Portal view : 

 

VishalBirajdar_1-1696339080863.png

 

 

Step 2 : Write Client script and script include as below 

 

Script include :

Name - assetDetailUtils

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


    getAssetDetails: function() {
        
		/*1. Get user from client script */
        var user = this.getParameter('sysparm_user');

        /*2. Declare one array to send results */
        var assetarray = [];
        
		/*3. Glide record on Hardware table - you can use your table */
        var grHardware = new GlideRecord('alm_hardware');
        grHardware.addQuery('assigned_to', user);  //query with assigned to is user
        grHardware.query();

        while (grHardware.next()) {
           /* 4. Get detalis in JSON */
            var assetDetails = {};
            assetDetails.asset_tag = grHardware.getValue('asset_tag').toString();
            assetDetails.serial_num = grHardware.getValue('serial_number').toString();
            

			/* 5. Push the JSON in Array */
            assetarray.push(JSON.stringify(assetDetails));
        }

        /* Return Array */
        return assetarray.join('|');

    },

    type: 'assetDetailUtils'
});

 

OnChange client script on 'User' variable :

 

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


    var user = g_form.getValue('user');
    var values;
    var ga = new GlideAjax('assetDetailUtils');   //script include
    ga.addParam('sysparm_name', 'getAssetDetails');  //script include function
    ga.addParam('sysparm_user', user);

    ga.getXMLAnswer(callBack);

    function callBack(answer) {

        var result = answer.split('|');
     
        for (var i = 0; i < result.length; i++) {

            if (values) {
                values = values + '<p>' + 'Asset Tag : ' +  JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+   JSON.parse(result[i]).serial_num +'</p>';
            } else {
                values = '<p>' + 'Asset Tag : ' +  JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+   JSON.parse(result[i]).serial_num +'</p>';
            }
        }
        g_form.setValue('my_assets', values);
    }

}

 

 

Output : 

 

VishalBirajdar_3-1696339809064.png

 

 

 

VishalBirajdar_2-1696339736191.png

 

 

Hope this works...!!!

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates