Auto Populate department from caller in incident form

Rahul RJ
Giga Sage
Giga Sage

I have custom department string field.(This is code is working fine for department as reference field but not for string field)I am having an issue   populate department based on Caller selection

Below script returning department sys_id instead of this, i want to populate department Name

Client Script details:

Type: onChange

Table: Incident

Field Name: Caller

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

          return;

    }

    var caller = g_form.getReference('caller_id');

g_form.setValue('u_custom_department',caller.department);

}

1 ACCEPTED SOLUTION

Explore Service
Tera Expert

Hi Rahul,



You have to write down script that return department name.Using GlideAjax you have to call it .On change of Caller you have write down the script .



Script Include:



var getDepartmentName = Class.create();


getDepartmentName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {



  getDept :function()


  {


  var name='';


  var id = this.getParameter('sysparm_id');


  var dep=new GlideRecord('cmn_department');


  dep.addQuery('sys_id', id);


  dep.query();


  if(dep.next())


  {


  name=dep.name;


  }


  return name;



  },


  type: 'getDepartmentName'


});



Client Script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue == '') {


  return;


  }



  var caller = g_form.getReference('caller_id');


  g_form.setValue('u_department',caller.department);


  var graj=new GlideAjax('getDepartmentName');


  graj.addParam('sysparm_name', 'getDept');


  graj.addParam('sysparm_id', caller.department);


  graj.getXML(HelloWorldParse);


  function HelloWorldParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


    g_form.setValue('u_custom_department', answer);



  }


}



Please like, mark as correct/helpful if it is right.


View solution in original post

10 REPLIES 10

Explore Service
Tera Expert

Hi Rahul,



You have to write down script that return department name.Using GlideAjax you have to call it .On change of Caller you have write down the script .



Script Include:



var getDepartmentName = Class.create();


getDepartmentName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {



  getDept :function()


  {


  var name='';


  var id = this.getParameter('sysparm_id');


  var dep=new GlideRecord('cmn_department');


  dep.addQuery('sys_id', id);


  dep.query();


  if(dep.next())


  {


  name=dep.name;


  }


  return name;



  },


  type: 'getDepartmentName'


});



Client Script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue == '') {


  return;


  }



  var caller = g_form.getReference('caller_id');


  g_form.setValue('u_department',caller.department);


  var graj=new GlideAjax('getDepartmentName');


  graj.addParam('sysparm_name', 'getDept');


  graj.addParam('sysparm_id', caller.department);


  graj.getXML(HelloWorldParse);


  function HelloWorldParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


    g_form.setValue('u_custom_department', answer);



  }


}



Please like, mark as correct/helpful if it is right.