Catalog client script to Auto populate Values

Appu
Tera Guru

Hi

I have 3 fields on my catalog item namely
1. Application (Reference to rec_application Table,drop down)
2.Level 1 user (Reference to sys_user Table)
3.Level 2 user (Reference to sys_user Table)  so on selection of Application, Level1 and Level2 users should populate in the catalog item form

In Application we have user fields as primary user and secondary user

the primary owner should be mapped to Level1 and secondary user to Level2.

here is my script help me with changes:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var app = g_form.getValue('application');

var gr = new GlideRecord('rec_application ');
gr.addQuery('sys_id', app);
gr.query();
if(gr.next()){
g_form.setValue('level_1',gr.prim_own);
g_form.setValue('level_1',gr.sec_own);
}

//Type appropriate comment here, and begin script below

}

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

It is not right to use GlideRecord in Client script, instead you can use the script below

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ref = g_form.getReference('application', getApp);
    }
function getApp(ref) {
       
g_form.setValue('level_1', ref.prim_own);
g_form.setValue('level_2',ref.sec_own);
    }

 

Also in your script

g_form.setValue('level_1',gr.prim_own);
g_form.setValue('level_1',gr.sec_own); //this line should have level_2, but you have repeated level_1

-Anurag

-Anurag

View solution in original post

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

It is not right to use GlideRecord in Client script, instead you can use the script below

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ref = g_form.getReference('application', getApp);
    }
function getApp(ref) {
       
g_form.setValue('level_1', ref.prim_own);
g_form.setValue('level_2',ref.sec_own);
    }

 

Also in your script

g_form.setValue('level_1',gr.prim_own);
g_form.setValue('level_1',gr.sec_own); //this line should have level_2, but you have repeated level_1

-Anurag

-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Either use getReference() with callback or GlideAJax instead of GlideRecord

Regards
Ankur

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

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Script seems fine with jsut the variable level1 being set & not level2. What issue you are stuck with?

Also, as Anurag suggested it is not ideal to use GlideRecord() in client script. You can use getReference() or GlideAjax.