The CreatorCon Call for Content is officially open! Get started here.

How to change the value of Assignment Group after onChange

pjftenorio
Giga Guru

I created a new tab in Form Section named User Details. Fields - User ID, Email, and Department was auto-populated after choosing Name of User. How do I get the Department value? I need to change the Assignment Group to 'IT Securities' when the Department is 'IT'.

 

Code below is not working.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var dept = g_form.getValue('department');
    alert(dept);
    if (dept === 'IT') {
        g_form.setValue('assignment_group', 'IT Securities');
    }
}
 
pjftenorio_0-1694575814269.png

 

pjftenorio_0-1694576198803.png

 

1 ACCEPTED SOLUTION

jonsan09
Giga Sage
Giga Sage

I'm guessing the Department field on your form is dot walked from the "Name of User" field. You can try to dot walk the "Name of user" field.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var dept = g_form.getReference('Name of user FIELD NAME').department;
    alert(dept);
    if (dept === 'IT') {
        g_form.setValue('assignment_group', 'IT Securities');
    }
}

View solution in original post

3 REPLIES 3

jonsan09
Giga Sage
Giga Sage

I'm guessing the Department field on your form is dot walked from the "Name of User" field. You can try to dot walk the "Name of user" field.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var dept = g_form.getReference('Name of user FIELD NAME').department;
    alert(dept);
    if (dept === 'IT') {
        g_form.setValue('assignment_group', 'IT Securities');
    }
}

Hi @jonsan09 ,

 

This getReference now works but I think it throws the sys_id of department. How do I convert this(9a7ed3f03710200044e0bfc8bcbe5db7) to actual word - 'IT'?

Apologies as I'm new in ServiceNow.

pjftenorio_0-1694577901709.png

 

You'll want to check your user table figure out table that department field is referencing and figure out the display field/name field for deparment.   From there I believe you can keep dotwalking for example, I'm using name as an example but it might be different in your instance.

var DeptName = g_form.getReference('Name of user FIELD NAME').department.name;