Catalog client script is not working in portal but working when I do a Try It in maintain items

Priyanka145
Tera Contributor

Hi All,

 

I have written a catalog client script to display asset information based on requested for field

It is working fine when I do a 'Try It' in catalog item , but not in service portal.

 

function onChange(control, oldValue, newValue, isLoading) {
      var rfor = g_form.getValue('requested_for');
    var grd = new GlideRecord('alm_hardware');
    grd.addQuery('assigned_to', rfor);
    grd.query();
    if (grd.next()) {
        var asset = grd.asset_tag;
    }
    g_form.setValue('device_information', asset);
}
 
This is an on change catalog client script written on requested for field.
Please guide
1 ACCEPTED SOLUTION

@Priyanka145 Try the below script to clear if the user doesn't have any asset attached in alm_hardware table.

 

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

    var rfor = g_form.getValue('requested_for');
    var grd = new GlideRecord('alm_hardware');
    grd.addQuery('assigned_to', rfor);
    grd.query(setAssetInfo);

    function setAssetInfo(grd) {
        if (grd.next()) {
            var asset = grd.asset_tag;
            g_form.setValue('device_information', asset);
        }else{
			g_form.setValue('device_information', "");
		}
    }

}

 

Can you please mark my answers helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

Hi @Priyanka145 

Have you selected UI Type as ALL ? If not change it to ALL and try once.

 

AnveshKumarM_0-1700551892518.png

 

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

 

Thanks,
Anvesh

Yes @AnveshKumar M  It is selected as All only. But still not working

@Priyanka145 

Then try this following script in your onChange client script.

 

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

    var rfor = g_form.getValue('requested_for');
    var grd = new GlideRecord('alm_hardware');
    grd.addQuery('assigned_to', rfor);
    grd.query(setAssetInfo);

    function setAssetInfo(grd) {
        if (grd.next()) {
            var asset = grd.asset_tag;
            g_form.setValue('device_information', asset);
        }
    }

}

 

I'm assuming device_information variable is Single Line text, if it is Reference type to alm_hardware table, use the following script.

 

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

    var rfor = g_form.getValue('requested_for');
    var grd = new GlideRecord('alm_hardware');
    grd.addQuery('assigned_to', rfor);
    grd.query(setAssetInfo);

    function setAssetInfo(grd) {
        if (grd.next()) {
            var asset = grd.sys_id;
            g_form.setValue('device_information', asset);
        }
    }

}

 

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

 

Thanks,
Anvesh

Hi @AnveshKumar M  It is a single line text, so the first script worked for me. Thanks.

If the asset is not assigned to any of the user in the requested for, if asset is empty. Then the older value has to be cleared. please let me know how it can be done