Cat Client Script Support

GMoon
Kilo Sage

Hi all,

I have a requirement on a Catalog Item to populate a field based on a prior selection. I have written a script but cannot get it to function, details below:

 

Variable name:  'server_to_enable' - this populates a list of configuration items.
Variable name:  'environment_to_enable' - we want this to populate with a list of environments that are associated with the earlier configuration items field. (this is the field that is not populating).

 

I've attached the script below, thanks.

1 ACCEPTED SOLUTION

Hello Ankur,

Thanks for the script details. I tested this out but it did not work unfortunately. I believe the script includes method may be the way forward.

View solution in original post

13 REPLIES 13

@GMoon 

2 approaches

1) onChange + GlideAjax script include

OR

2) onChange + GlideRecord callback

try this 2nd approach I shared

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    var vServer = g_form.getValue('server_to_enable');
    var gr = new GlideRecord("cmdb_ci_rel");
    gr.addQuery("child", vServer);
    gr.addQuery("parent.sys_class_name", "cmdb_ci_environment");
    gr.query(checkRecord);

    function checkRecord(gr) {
        if (gr.next()) {
            if (gr.parent)
                g_form.setValue('environment_to_enable', parent);
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hello Ankur,

Thanks for the script details. I tested this out but it did not work unfortunately. I believe the script includes method may be the way forward.

@GMoon 

it should work now

1) the table name is cmdb_rel_ci and not cmdb_ci_rel

2) also I was giving wrong value during setValue()

Updated script below

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    var vServer = g_form.getValue('server_to_enable');
    var gr = new GlideRecord("cmdb_rel_ci");
    gr.addQuery("child", vServer);
    gr.addQuery("parent.sys_class_name", "cmdb_ci_environment");
    gr.query(checkRecord);

    function checkRecord(gr) {
        if (gr.next()) {
            if (gr.parent)
                g_form.setValue('environment_to_enable', gr.parent);
        }
    }
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

Thanks for the updated script. I tested without success unfortunately.

Viraj Hudlikar
Tera Sage

Hello @GMoon 

Did you tried to see what value is coming up by putting some alert statements.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.