Autopopulate fields on incident form

Sreevani K
Kilo Contributor

I have a field named requester which i created with reference to user table.Based on the selection of user's name in the requester field it should auto populate other fields like company,location and contact number in the incident form.Please suggest how to write On change client script for the above requirement.

2 REPLIES 2

Tricia2
Mega Contributor

You would want to create a GlideAjax call to a scriptInclude to get the referenced field values to autoPopulate.

clientScript example:

//call to getUserData script include userInfo method
//Populates the Users Title and department

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

clearlUserInfo(); //if the user field changes you want to get the new users data

if (newValue == '') {
return;
}

var ga = new GlideAjax('getUserData');
ga.addParam('sysparm_name','userInfo');
ga.addParam('sysparm_userid', newValue); //this is the value of the field selected on the onChange Client Script
ga.getXML(fillUserInfo);

}

function clearlUserInfo() {
g_form.clearValue('your field name');
g_form.clearValue('the users populated field name');


}

function fillUserInfo(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON(); //Transform the JSON string to an object

g_form.setValue('your field name',answer.title);
g_form.setValue('your field name',answer.department);


}

 

scriptInclude example:

Name: getUserData

Client Callable: Checked

//commented added
var getUserData = Class.create();
getUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userInfo : function() {

var retObj = {};
var sysId = this.getParameter('sysparm_userid');

var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", sysId);
gr.query();
if(gr.next() ) {
retObj.title = gr.getValue('title');
retObj.department = gr.getDisplayValue('department');//if it is another reference like location is

var json = new JSON();
return json.encode(retObj);
}

},

type: 'getUserData'
});

 

Hope this helps

 

Archana Reddy2
Tera Guru

Hi Ganesh,

A single onChange client script can do this for you.

Set Field Name as Requester.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   var user = new GlideRecord('sys_user');
user.addQuery('sys_id',newValue);
user.query();
if(user.next())
{
	g_form.setValue('mail',user.email);//set values as required
	}}

Let me know if you need any further assistance on this.

Thanks,

Archana