- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I have written script include and catalog client script to populate the user id on selection of user name. But not getting the correct value on user id field.
Anyone have idea on this.
Script include:
var populateUserID = Class.create();
populateUserID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
details: function() {
var userObj = {};
var gr = new GlideRecord('sys_user');
if (gr.get(this.getParameter('sysparm_userSelected'))) {
userObj.gruserid = gr.user_name.getDisplayValue();
}
return JSON.stringify(userObj);
},
type: 'populateUserID'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
//g_form.clearValue('UserIDornumber');
return;
}
var name = g_form.getValue('userName');
var userDetails = new GlideAjax('populateUserID');
userDetails.addParam('sysparm_name', 'details');
userDetails.addParam('sysparm_userSelected', name);
userDetails.getXMLAnswer(function(response) {
var userObj = JSON.parse(response);
g_form.setValue('UserIDornumber', userObj.gruserid);
});
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
if this is for catalog item then you can use Auto populate feature with no scripting
Auto-populate a variable based on a reference type variable (Utah)
If you still want to use script then update as this
Script include: no need to return json as you are returning only 1 value
var populateUserID = Class.create();
populateUserID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
details: function() {
var gr = new GlideRecord('sys_user');
if (gr.get(this.getParameter('sysparm_userSelected'))) {
return gr.user_name.toString();
}
},
type: 'populateUserID'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '')
g_form.clearValue('UserIDornumber');
var name = g_form.getValue('userName');
var userDetails = new GlideAjax('populateUserID');
userDetails.addParam('sysparm_name', 'details');
userDetails.addParam('sysparm_userSelected', name);
userDetails.getXMLAnswer(function(response) {
g_form.setValue('UserIDornumber', response);
});
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
userObj.gruserid = gr.getValue('user_name'); // try this. since user_name is string fiekd DisplayValue is not required, rest you code seems fine.
// also avoid using gr as variable.
// make user AA has valid value in user_name
is the field/variable "userName" a reference field referring to user tabkle.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Thank you for your response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
I use the following in a client script to parse an object returned by my script include:
function EmployeeDetailsLookup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
// set from fields based on the response...
}
what you have works for me when a single value is returned by the script include. Also, I use:
ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous call
in the client script to call the function to process the result.
But you're returning one value, just return the value, not an object.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @nisargac21
The issue is in your scrip include in this part
if (gr.get(this.getParameter('sysparm_userSelected'))) {
userObj.gruserid = gr.user_name.getDisplayValue();
}
gr.get() expects a sys_id of the user record, but in your Client Script you’re passing g_form.getValue('userName') which is most likely not the sys_id but instead the Name or Display Value of the user.
So your gr.get() call often fails and you don’t get the correct user_name.
See the correct code:
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var userSysId = g_form.getValue('userName'); // sys_id from reference field
var ga = new GlideAjax('populateUserID');
ga.addParam('sysparm_name', 'details');
ga.addParam('sysparm_userSelected', userSysId);
ga.getXML(updateCampus);
function updateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var userObj = JSON.parse(answer);
// Use whichever options you need
g_form.setValue('UserIDornumber', userObj.option_1);
g_form.setValue('userEmail', userObj.option_2);
g_form.setValue('userFullName', userObj.option_3);
g_form.setValue('userDepartment', userObj.option_4);
}
}
Include Script
var populateUserID = Class.create();
populateUserID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
details: function() {
var userObj = {};
var userSysId = this.getParameter('sysparm_userSelected');
var gr = new GlideRecord('sys_user');
if (gr.get(userSysId)) {
userObj.option_1 = gr.user_name.toString(); // login ID
userObj.option_2 = gr.email.toString(); // email
userObj.option_3 = gr.name.toString(); // full display name
userObj.option_4 = gr.department.getDisplayValue(); // department name
}
return JSON.stringify(userObj); // return as JSON
},
type: 'populateUserID'
});
See the reference to how create a glideAjax
GlideAjax Example Cheat Sheet - ServiceNow Community