Convert Given OnChange GlideRecord Client Script to GlideAjax so Script Works while Impersonating End User

alantpham
Kilo Contributor

The following OnChange client script is being used for a catalog item I'm working on and works for admins but not for end users I try to impersonate while testing. I believe using a GlideAjax approach would work but I can't figure out how to convert this script to GlideAjax to achieve the same result. Please rescript and explain.

 

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

var user = new GlideRecord('sys_user');
user.addQuery('sys_id',newValue);
user.query(setOnBehalfDetails);
function setOnBehalfDetails(user) {
if(user.next())
{

g_form.setValue('user_id',user.user_name);

9 REPLIES 9

Hi,

Yes. It should be Catalog Client scripts not a client scripts as you have to auto-populate the Catalog variables not a RITM fields.

Refer docs - Catalog client scripts

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Tudor
Tera Guru

Hello,

Question: are you using this for submission on a record producer/cat item to store the information for whom the record has been raised?

If so, the field in your resulting record might be populated after submission from elsewhere(workflow, flow, business rule). As such, even if you update the value front-wise, after submission, it might be even before insert due to a BR, the value gets changed.

Please also check this.

Tudor

 

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Alantpham

I've tested the client side GlideRecord script in my instance and it works for both admin and non-admins.

If it's not working for non-admins, the problem is not because it's not using Scriplet Include.

Can it be that non-admin are opening the form from Service Portal? If so, UI Type should be set to "all" in Client Script.

find_real_file.png

 

Hi again,

Check the non-admin user has sufficient role to view Service Catalog form. That is, the user is able to navigator to the Service Catalog from home page. Then check if the user is able to view User table. Enter "sys_user.list" in Application Navigator to see if user list shows. If the user list does not show, it implies the user does not have sufficient privilege.

find_real_file.png

Then try changing the script as follows. When onChange is executed, it should show an alert message and if there is an error executing the script, it'll show an error message.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    alert("in onChange");
    try {
        var user = new GlideRecord('sys_user');
        user.addQuery('sys_id', newValue);
        user.query(setOnBehalfDetails);

        function setOnBehalfDetails(user) {
            if (user.next()) {
                g_form.setValue('user_id', user.user_name);
            }
        }
    } catch (e) {
        alert(e.message);
    }
}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Few things to check

1) Is the script include function getting called for non-admins?

2) if yes then are they able to query sys_user table or not?

3) did you add getRowCount() to verify the count of record

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader