Hi Everyone facing issue in the Client script

rajeshKongamudi
Tera Contributor

Hi Community,

 

I hope you doing well, we have a client script that is auto populating asset details using script include as shown below. here the problem we are facing is when we are changing the username who is having asset details it is getting populating but when we are changing the username who don't have assets, we are facing problem, and it is showing

this error(Error There is a JavaScript error in your browser console). Please can someone please help me on this. thanks in advance.

Here is the Script include:

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

    findUserswithMoreThan1Assets: function() {     
  //var arr = [];
  var id = this.getParameter('sysparm_userID');
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', id);
        user.query();
        if(user.next()){
            var aseetList = new GlideRecord('cmdb_ci_computer');
            aseetList.addQuery('assigned_to', user.sys_id);
            aseetList.addQuery('install_status''1');
            aseetList.addQuery('operational_status''1');
            aseetList.orderByDesc('u_last_check_in');
            aseetList.query();
            if(aseetList.next()){
                var asset = aseetList.asset_tag+ ',' +aseetList.serial_number+ ',' +aseetList.model_id.getDisplayValue()+ ',' +aseetList.name;
                //var assetname = ;
            }
        }
return asset;
    },
    type: 'getassetsforusers'
});

 

 

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    // if(newValue == '')
    //      g_form.clearValue('asset_name'); // give correct variable name here

    //  if(oldValue != newValue){

    var AssetUser = new GlideAjax('getassetsforusers');
    AssetUser.addParam('sysparm_name'"findUserswithMoreThan1Assets");
    AssetUser.addParam('sysparm_userID', newValue);
    AssetUser.getXML(setValue);

    function setValue(response) {
        alert('function');
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert('function ' +answer);
        if (answer != '') {
            var spl = answer.split(',');
            g_form.setValue('asset_name', spl[3]);
            g_form.setValue('serial_number', spl[1]);
            g_form.setValue('model_id', spl[2]);
            g_form.setValue('asset_tag', spl[0]);
            // give correct variable name here
        } else if(oldValue != newValue){ //Here in else condition it is making some problem when we are trying to change it to empty asset user
            alert('function ' +answer);
            g_form.setValue('asset_name''');
            g_form.setValue('serial_number''');
            g_form.setValue('model_id''');
            g_form.setValue('asset_tag''');
        }
    }

 

 

1) The person having asset details (Mohamed Cheikhrouhou)

 

rajeshKongamudi_0-1695646219407.png

 

2) These are the asset details

rajeshKongamudi_1-1695646275683.png

 

3) The person doesn't having asset details (Rajesh kongamudi) here it is showing console error

rajeshKongamudi_2-1695646390664.png

 

4) Not making those assett details empty and keeping the same assett details having the previous person asset details

rajeshKongamudi_3-1695646859536.png

 

 

 

 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @rajeshKongamudi ,

 

Try changing your script include to the script below, and try. The error is because of returning a variable which is not defined if the user have no asset.

 

 

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

    findUserswithMoreThan1Assets: function() {     
  //var arr = [];
  var id = this.getParameter('sysparm_userID');
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', id);
        user.query();
		var asset = '';
        if(user.next()){
            var aseetList = new GlideRecord('cmdb_ci_computer');
            aseetList.addQuery('assigned_to', user.sys_id);
            aseetList.addQuery('install_status', '1');
            aseetList.addQuery('operational_status', '1');
            aseetList.orderByDesc('u_last_check_in');
            aseetList.query();
            if(aseetList.next()){
                asset = aseetList.asset_tag+ ',' +aseetList.serial_number+ ',' +aseetList.model_id.getDisplayValue()+ ',' +aseetList.name;
                //var assetname = ;
            }
        }
return asset;
    },
    type: 'getassetsforusers'
});

 

  

Please mark my answer helpful and accept as solution if it helped you 👍✔️

Thanks,
Anvesh

View solution in original post

2 REPLIES 2

AnveshKumar M
Tera Sage
Tera Sage

Hi @rajeshKongamudi ,

 

Try changing your script include to the script below, and try. The error is because of returning a variable which is not defined if the user have no asset.

 

 

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

    findUserswithMoreThan1Assets: function() {     
  //var arr = [];
  var id = this.getParameter('sysparm_userID');
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', id);
        user.query();
		var asset = '';
        if(user.next()){
            var aseetList = new GlideRecord('cmdb_ci_computer');
            aseetList.addQuery('assigned_to', user.sys_id);
            aseetList.addQuery('install_status', '1');
            aseetList.addQuery('operational_status', '1');
            aseetList.orderByDesc('u_last_check_in');
            aseetList.query();
            if(aseetList.next()){
                asset = aseetList.asset_tag+ ',' +aseetList.serial_number+ ',' +aseetList.model_id.getDisplayValue()+ ',' +aseetList.name;
                //var assetname = ;
            }
        }
return asset;
    },
    type: 'getassetsforusers'
});

 

  

Please mark my answer helpful and accept as solution if it helped you 👍✔️

Thanks,
Anvesh

Mayur2109
Kilo Sage
Kilo Sage

Hi @rajeshKongamudi ,

 

Have you tried else if(answer == '') ?

 

Regards,

Mayur