Auto-populate string field based on a reference table

khyati_thadani
Tera Expert

I create a field called 'data' on an incident table, the field type is a list type , giving reference user table. When we select a user in the list collector same user should be populated into the string field(description or short description or any other string field) present in the same incident form. How to solve this by glideAjax? I have written a script include and called using the client script. Can you tell me where I am going wrong?

SCRIPT INCLUDE:

 

Name: GetName

API Name: global.GetName

Client Callable (checked)

var GetName = Class.create();
GetName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getName: function() {
var userRecord = new GlideRecord("sys_user");
userRecord.get(this.getParameter('sysparm_userID'));
return userRecord.name + '';
},

type: 'GetName'
});

 

CLIENT SCRIPT:

 

Table : incident

Type : onChange

Field Name : Data (custom field with field type as 'list')

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Instantiate the GetName Script Include
var getName = new GlideAjax('GetName');
// Specify the getName method
getName.addParam('sysparm_name', 'getName');
// Pass the Requested for sys_id
getName.addParam('sysparm_userID', g_form.getValue('u_data')); // variable value
// Send the request to the server
getName.getXML(populateNameField);


// When the response is back from the server
function populateNameField(response) {
// Extract the name from the response, clear any value from the name field,
// set new value in the description field
var nameFromScriptInclude = response.responseXML.documentElement.getAttribute("answer");
g_form.clearValue('description');
g_form.setValue('description', nameFromScriptInclude);
}
}

 

1 ACCEPTED SOLUTION

Hi @khyati_thadani ,
Try this! Hope it works!
Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var getName = new GlideAjax('GetName');
getName.addParam('sysparm_name', 'getUserName');
getName.addParam('sysparm_userID', newValue);
getName.getXML(populateNameField);
function populateNameField(response) {
var nameFromScriptInclude = response.responseXML.documentElement.getAttribute("answer");
g_form.clearValue('description');
g_form.setValue('description', nameFromScriptInclude);
}
}

Script Include:

var GetName = Class.create();
GetName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserName: function() {
var t = '';
var arr = [];
var userRecord = new GlideRecord("sys_user");
arr = this.getParameter('sysparm_userID');
userRecord.addEncodedQuery('sys_idIN' + arr.toString());
userRecord.query();
while(userRecord.next())
{
t = t +" " + userRecord.getDisplayValue();
}
return t;
},
type: 'GetName'
});
Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

View solution in original post

7 REPLIES 7

Rahul Talreja
Mega Sage
Mega Sage

Hi @khyati_thadani ,
Try with 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Instantiate the GetName Script Include
var getName = new GlideAjax('GetName');
// Specify the getName method
getName.addParam('sysparm_name', 'getName');
// Pass the Requested for sys_id
//getName.addParam('sysparm_userID', g_form.getValue('u_data')); // variable value
getName.addParam('sysparm_userID', newValue);
// Send the request to the server
getName.getXML(populateNameField);


// When the response is back from the server
function populateNameField(response) {
// Extract the name from the response, clear any value from the email field,
// set new value in the description field
var nameFromScriptInclude = response.responseXML.documentElement.getAttribute("answer");
g_form.clearValue('description');
g_form.setValue('description', nameFromScriptInclude);
}
}
var GetName = Class.create();
GetName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getName: function() {
var t = '';
var arr = [];
var userRecord = new GlideRecord("sys_user");
arr = this.getParameter('sysparm_userID').split(',');
userRecord.addEncodedQuery('sys_idIN' + arr.toString());
userRecord.query();
while(userRecord.next())
{
t = t + userRecord.getValue('name').toString();
}
return t;
},

type: 'GetName'
});

 

 

 

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Hey @Rahul Talreja,

 

I tried the changes you mentioned. The string field (for example description) should populate with the value present in the field (data) on change but it is not happening. I have attached a video for your reference.

 

 

Hi @khyati_thadani , I have edited the script, Can you try once with the new script!

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Hey @Rahul Talreja,

I tried again but no luck!