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

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

Harish Bainsla
Kilo Patron
Kilo Patron

You can use script include  I am sharing a link go through it will  help you

https://www.servicenow.com/community/developer-forum/auto-populate-a-string-field-based-on-reference...

Hey @Harish Bainsla,

I have seen the same link, modified my code instead of the email address I want the name that I have chosen in that custom field 'data' to be populated in the string field, and tried but not getting any response.