How to populate choice filed value based on other field

seenu_pvr
Tera Contributor
Dear Friends,
 
I have a on change client script where I need to populate the channel field as self service when the caller id role is ITIL. but I cant populate the channel field as self servcie. please advise. Here is the On change script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }


    var gr = new GlideRecord('sys_user_has_role');
    gr.addQuery('user.sys_id', newValue);
    gr.addQuery('role.name', 'itil');
    gr.query();
    //g_form.addInfoMessage(newValue);
    if (gr.next()) {
        g_form.getOption('contact_type', 'self-service');
       // g_form.addInfoMessage("iam in if loop");

    } else {
        g_form.getOption('contact_type', 'None');
       // g_form.addInfoMessage("I am in else loop");
    }
3 REPLIES 3

Brad Bowman
Mega Patron

GlideRecord is not recommended nor supported in client scripts.  They can work sometimes, but not when dot-walking field names like you are doing here.  The correct way to approach this is by calling a Script Include with GlideAjax, passing in the newValue.  The SI script can then execute the GlideRecord to check for the ITIL role, returning 'self-service' (if that's the correct choice value) or '' for - None - using g_form.setValue('contact_type', <<answer returned from SI>>).

 

Here's an excellent guide on GlideAjax

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

Hi Brad,

 

Thanks For your reply. I made it. I can use form. setValue method to acheive this.

g_form.setValue('contact_type','Reset','Reset Reason');

 

Uh, yeah, but how does this satisfy the requirements of the question - to populate the choice value based on other field?