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

Mayur2109
Kilo Sage

Hi @servicenow_devo ,

 

Check if below community post helps you, seems your requirement matches with the same.

https://www.servicenow.com/community/incident-management-forum/display-all-the-assets-assigned-to-us... 

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

Manoj89
Giga Sage

set onCHange script for the field requested_for if that is where you are going to change users

function onChange(control, oldValue, newValue, isLoading) {
var assets = [];
var ol;
var gr = new GlideRecord('cmdb_ci_computer');
gr.query('assigned_to', newValue);
gr.query();
while (gr.next()){
    assets.push(gr.asset_tag);
}

//add to your form a rich text label variable
if(assets.length >0){
   assets.forEach(createOl);
   g_form.setDisplay('rich_text', true);
   g_form.setLabelOf('rich_text', 'Dear,<br/><br/>You already have PC(s)<br/><ol>'+ol+'</ol> assigned');
}else{
   g_form.setDisplay('rich_text', false);
}
}

function createOl(element, index){
      ol +=  '<li id='+index+'>'+element+'</li>';
}

 Note: this script was written considering maintain item form.

Hi thanks for reply.

The logic seems nice and but I was not able to get the output. It's not showing any text box for the requirement.

Thankyou

Hi,

 

Poor me, using glide record in client script is not recommended, can you please move the glide record script to script include and call it using glideajax, it's working perfectly for me, you can do debugging and check what you are getting as a result.