How to refer to a table record from a client script and automatically set choices accordingly

d-aizawa
Kilo Sage

I want to automatically set the choices of a certain field subject to Incident's Assignment Group.
I added two fields as a preparation for the script.

Test
Column name : u_test
Table : sys_user_group
Type : String
Choice value:

  • jac
  • ams
  • sysop

Management
Column name : u_management
Table : inciddent
Type : String
Choice value:

  • question
  • company
  • adm
  • sys

For example, when a group with jac set in the Test field is entered in the Assignment Group,
the options in the Management field are automatically set in company.

And if nothing is set, I want the question to be set automatically.

I wrote a script, but when I enter a group in Assignment Group, it becomes a question.

Help me.
Thank you.

Client Script : (Field name : Assignment group)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var kannri = g_form.getValue('assignment_group');
    var ajax = new GlideAjax('GetGroupInfo');
    ajax.addParam('sysparm_name', 'GetGroupTest');
    ajax.addParam('sysparm_GroupID', kannri);
    ajax.getXML(GetGroupInfo);

    function GetGroupInfo(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'jac') {
            g_form.setValue('u_management', 'company');
        }
        if (answer == 'ams') {
            g_form.setValue('u_management', 'adm');
        }
        if (answer == 'sysop') {
            g_form.setValue('u_management', 'sys');
        } else {
            g_form.setValue('u_management', 'question');
        }
    }
}

Script Include

Accessible from : All application scopes

Client callable : True

var GetGroupInfo = Class.create();
GetGroupInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    GetGroupTest: function() {
        var groupRecord = new GlideRecord('sys_user_group');
        groupRecord.get(this.getParameter('sysparm_GroupID'));
        return groupRecord.u_test;
    },
    type: 'GetGroupInfo'
});

incident 

assignment_group : company

assignment_group.test : jac

find_real_file.png

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

You should return string from the script include to the client script.

I also don't see any logging to help you troubleshoot. It's always best to add some logging so you can see that everything is working as you'd prefer, else you're just coding in the dark.

With the above said here's a few things to review:

  • Ensure that the onChange client script is set on the correct field (just saying this for the record)
  • In your script include, consider double-checking the value and typeof for this: this.getParameter('sysparm_GroupID')
  • For your return in the script include, as mentioned above, it needs to be string, such as:
    return groupRecord.u_test.toString();
    
    or 
    
    return groupRecord.getValue('u_test');​
  • For your client script and the answer evaluation, your if statements don't necessarily equal out, meaning your "else" is only applied to the last if statement versus as a default if nothing else matches, considering reviewing and adjusting such as:
if (answer == 'jac') {
g_form.setValue('u_management', 'company');
} else if (answer == 'ams') {
g_form.setValue('u_management', 'adm');
} else if (answer == 'sysop') {
g_form.setValue('u_management', 'sys');
} else {
g_form.setValue('u_management', 'question');
}

In the future, if the selections become a bit longer than this, consider using a JavaScript switch. For now though, it's fine as is.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi,

You should return string from the script include to the client script.

I also don't see any logging to help you troubleshoot. It's always best to add some logging so you can see that everything is working as you'd prefer, else you're just coding in the dark.

With the above said here's a few things to review:

  • Ensure that the onChange client script is set on the correct field (just saying this for the record)
  • In your script include, consider double-checking the value and typeof for this: this.getParameter('sysparm_GroupID')
  • For your return in the script include, as mentioned above, it needs to be string, such as:
    return groupRecord.u_test.toString();
    
    or 
    
    return groupRecord.getValue('u_test');​
  • For your client script and the answer evaluation, your if statements don't necessarily equal out, meaning your "else" is only applied to the last if statement versus as a default if nothing else matches, considering reviewing and adjusting such as:
if (answer == 'jac') {
g_form.setValue('u_management', 'company');
} else if (answer == 'ams') {
g_form.setValue('u_management', 'adm');
} else if (answer == 'sysop') {
g_form.setValue('u_management', 'sys');
} else {
g_form.setValue('u_management', 'question');
}

In the future, if the selections become a bit longer than this, consider using a JavaScript switch. For now though, it's fine as is.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hitoshi Ozawa
Giga Sage
Giga Sage

Following script is overwriting u_management in the last "else" statement.

        if (answer == 'jac') {
            g_form.setValue('u_management', 'company');
        }
        if (answer == 'ams') {
            g_form.setValue('u_management', 'adm');
        }
        if (answer == 'sysop') {
            g_form.setValue('u_management', 'sys');
        } else { // if answer == 'ams' or 'jac' following condition will also be executed
            g_form.setValue('u_management', 'question');
        }

Try:

        if (answer == 'jac') {
            g_form.setValue('u_management', 'company');
        } else if (answer == 'ams') {
            g_form.setValue('u_management', 'adm');
        } else if (answer == 'sysop') {
            g_form.setValue('u_management', 'sys');
        } else {
            g_form.setValue('u_management', 'question');
        }