- 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 02:06 PM
Hello,
What do your logs say? I see you have several in there, but you don't tell us anything about that side of things.
As far as back in your client script, where you are taking the answer you get back from SI and doing something with it, I think you need to swap out the var aObj line and do this instead:
answer = JSON.parse(answer);
then just use answer.uu_title etc for setting field values.
You'd want to set an alert(answer); below where you're setting the answer variable to your response so you can see if you got the JSON format coming back from Script Include. If so...then the line I said above to add should do the trick for you.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2019 06:32 PM
Glad you got it resolved...
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2019 02:17 PM
If you just need couple of fields than you can just use below script to get that info without script includes
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getReference(g_form.getValue('requested_for'), populateReqForDetails);
function populateReqForDetails(user) {
g_form.setValue('emp_position', user.title);
g_form.setValue('emp_id', user.user_name);
}
}
- 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