- 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
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
Thank you for your response, I tried the 1st solution, Auto-populate a variable based on a reference type. It worked.