If the user has asset then in the catalog item one variable should be visible

Servicenow de11
Tera Contributor

Hello,

 

I have a requirement that is if the user has assigned asset then in the catalog item- one variable should be visible and if the user has no asset that variable should disappear. I have tried Below script but it is not working . Can anyone let us know how to achieve this.

 

Script include:

 

var CheckAssetOwnershipAjax = Class.create();
CheckAssetOwnershipAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    userHasAsset: function() {
        var userId = this.getParameter('userId');
        var assetGr = new GlideRecord('alm_asset');
        assetGr.addQuery('assigned_to', userId);
        assetGr.query();
        return assetGr.hasNext() ? 'true' : 'false';
    }
});
 
ONChange Client script on person_terminated variable
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var variableName = 'who_is_it_s_point_of_contact_to_recover_the_terminated_user_s_asset'
    var userId = g_form.getValue('person_terminated');

    
    var ga = new GlideAjax('CheckAssetOwnershipAjax');
    ga.addParam('sysparm_name', 'userHasAsset');
    ga.addParam('userId', userId);
    ga.getXMLAnswer(function(response) {
        if (response == 'true') {
            g_form.setVisible(variableName, true);
        } else {
            g_form.setVisible(variableName, false);
        }
    });
}
 
Thanks in advance.
2 REPLIES 2

Anubhav24
Mega Sage
Mega Sage

Hi @Servicenow de11 ,

In your script include in the return statement you have used hasnext() which itself returns true or false , I think you do not need a ternary operator over there you can simply right return assetGr.hasNext().

Also in your Client Script can you check/debug what value is being returned in the variable "response". And if you are trying to compare boolean value with double quotes just check if it works without double quotes or maybe use .toString function before comparing the value in if condition.

 

Please mark helpful/correct if my response helped you.

 

Hello Anubhav,

 

Can you please give me the modified script.

 

Thanks