client script: set assigned to on change assignment group

wc99
Tera Contributor

Hi, I have a requirement, after select an assignment group, one of the members from the group should auto populate. If the assignment group field clear again, the assigned to field will be cleared also. I am new to ServiceNow, and any help is appreciate, thanks! 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {  
         if (isLoading || newValue === '') {
                   return;
         }
        
         var gr = new GlideRecord('sys_user_grmember');
    
  gr.addQuery('group', g_form.getValue('assignment_group'));
         gr.query(function(_gr) {
                   if (_gr.next()) {
                          
        g_form.setValue('assignment_group', _gr.group.toString());
                   }
         });
}

1 ACCEPTED SOLUTION

you may try like,

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

if (newValue != ''){
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', newValue);
gr.query();
if (gr.next())
g_form.setValue('assigned_to', gr.user);
}else if (newValue == '') {
g_form.clearValue('assigned_to');
}

}

View solution in original post

5 REPLIES 5

This works perfect, thank you Shishir!