- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2019 01:57 PM
I am trying to keep two fields in a Catalog Item updated based on a "requested for" field (Reference to sys_user).
- Employee ID (emp_id) >> Default value: javascript:gs.getUserName();
- Employee Position (emp_position) >> Default value: javascript:gs.getUser().getRecord().getValue('title');
I am using a Script Include and an OnChange Client Script to keep these fields updated as the "requested for" field changes. My issue is that while it works on the Service Catalog side, when I try it in the Service Portal I get an error:
Here is the Script Include:
var UpdateUserValues = Class.create();
UpdateUserValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo : function() {
var userId = this.getParameter('sysparm_user_id');
var u = new GlideRecord('sys_user');
gs.log('userId=' + userId);
var uObj = {};
if (u.get(userId)) {
gs.log('user='+user.name+' title=' + u.title);
uObj = {
"uu_title" : u.getValue('title'),
"uu_userid" : u.getValue('user_name'),
};
}
var answer = new JSON().encode(uObj);
gs.log('answer=' + answer);
return answer;
},
type: 'UpdateUserValues'
});
And the Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
var ga = new GlideAjax('UpdateUserValues');
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('sysparm_user_id', g_form.getValue('requested_for'));
ga.getXML(updateFields);
}
function updateFields(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var aObj = answer.evalJSON(true);
g_form.setValue('emp_position', aObj.uu_title);
g_form.setValue('emp_id', aObj.uu_userid);
}
Any help will be greatly appreciated!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2019 02:31 PM
How about?
var UpdateUserValues = Class.create();
UpdateUserValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo : function() {
var userId = this.getParameter('sysparm_user_id');
var u = new GlideRecord('sys_user');
gs.log('userId=' + userId);
var uObj = {};
if (u.get(userId)) {
gs.log('user='+user.name+' title=' + u.title);
uObj = {
"uu_title" : u.getValue('title'),
"uu_userid" : u.getValue('user_name'),
};
}
return JSON.stringify(uObj);
},
type: 'UpdateUserValues'
});
CS:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
var ga = new GlideAjax('UpdateUserValues');
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('sysparm_user_id', g_form.getValue('requested_for'));
ga.getXML(updateFields);
}
function updateFields(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var data = JSON.parse(answer);
g_form.setValue('emp_position', data.uu_title);
g_form.setValue('emp_id', data.uu_userid);
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2019 05:30 PM
Thanks Prateek! This worked and helped me understand where my problem was.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2023 06:31 AM
Hi Prateek,
I am using a similar CS as above but it is not working in the service portal, I believe the documentElement is the issue. Is there a wat to use getElementById for the attributes inside the function?