Auto fill Assigned to field Client Script

masoud
Tera Contributor

The below onChange client script on "Assignment group" field will set the "Assigned to" field to the "Assignment group manager" whenever the Assignment group value changes. I'm trying to change this behavior to when the "Assignment group" is the same then keep the existing "Assigned to" value, and when "Assignment group" changes then clear the "Assigned to" field. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
ScriptLoader.getScripts('sn_hr_core.utils_ui.jsdbx', function(){
getGlideRecord('sys_user_group',newValue , setAssignTo);
});
function setAssignTo(group){
if (group)
g_form.setValue('assigned_to', group.manager);
}

 

Thanks,

Masoud

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Masoud,

Include if loop to trigger when assignment group is changed with a new value.

if(g_form.getValue('assignment_group') != oldValue)

{

//your logic

}

View solution in original post

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Masoud,

Include if loop to trigger when assignment group is changed with a new value.

if(g_form.getValue('assignment_group') != oldValue)

{

//your logic

}

Sajilal
Mega Sage

Hi Masood,

Please try with the below code.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '' || newValue!=oldValue) {
return;
}
ScriptLoader.getScripts('sn_hr_core.utils_ui.jsdbx', function(){
getGlideRecord('sys_user_group',newValue , setAssignTo);
});
function setAssignTo(group){
if (group)
g_form.setValue('assigned_to', group.manager);
}

Please Mark as Correct if this solves your issue and also mark ???? Helpful if it helps resolve your problem.

Thanks,
Saji

Gaurav Shirsat
Mega Sage

Hi 

You can achieve this using the Assignment Rule also.

If you want to map this for specific type of work,then you can do this.

else similar kind of functionality I did for my requirement,you can take help of my script

onchange:-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga=new GlideAjax('setAssignmentGroup');//script include name
ga.addParam('sysparm_name','populateAssignmentGroup');//script include function name
ga.addParam('sysparm_cmdb_ci',g_form.getValue('cmdb_ci'));//parameter passeed
ga.getXML(getResponse);

function getResponse(response)
{
var answer=response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('assignment_group',answer);
}

//Type appropriate comment here, and begin script below

}

Script Include:-Client Callable

var setAssignmentGroup = Class.create();
setAssignmentGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

populateAssignmentGroup : function(){
var gr=new GlideRecord('cmdb_ci');
gr.addQuery('sys_id',this.getParameter('sysparm_cmdb_ci'));
gr.query();
if(gr.next())
{
return gr.support_group;
}

},
type: 'setAssignmentGroup'
});

 

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat