- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2017 11:12 AM
I am trying to get the department and group return from the below script include. My logs entries are as follows. You can see the JSON values are empty even though I'm logging the dept and the group.
Any idea why the encoding is not working on my object.
Information | Answer: {"department":{},"group":{}} | SI_SetAssignedToFields | |||
Information | Dept: 33ff81966f3e0a00a2b7a4a21c3ee4d8, Group: 954ecdd76f9386006ceb381f5d3ee41a | SI_SetAssignedToFields |
Information | Team: 466497a21303120087de77f1f244b0d2 | SI_SetAssignedToFields |
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2017 01:09 PM
When ever you are doing a loop to push values it is very important that you use toString() or + "" to the elements you are pushing to an array or object. That forces the code to go back to the server to get the value. I have had cases where the same value is repeated.
Try the following:
var SI_SetAssignedToFields = Class.create();
SI_SetAssignedToFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssignedToFields: function () {
var assignedToTeam = this.getParameter('sysparm_assignedToTeam');
//gs.log('Inside function ', 'CBT_SI_SetAssignedToFields');
var obj = {};
if (assignedToTeam != '') {
gs.log('Team: ' + assignedToTeam, 'SI_SetAssignedToFields');
var grp = new GlideRecord('sys_user_group');
grp.addQuery('sys_id', assignedToTeam);
grp.query();
while (grp.next()) {
obj.department = grp.u_department.toString();
obj.group = grp.u_group.toString();
}
}
else {
obj.department = "";
obj.group = "";
}
gs.log('Dept: ' + obj.department + ', Group: ' + obj.group, 'SI_SetAssignedToFields');
var json = new global.JSON().encode(obj);//JSON formatted string
gs.log('Answer: ' + json, 'SI_SetAssignedToFields');
return json;
},
type: 'SI_SetAssignedToFields'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2017 01:09 PM
When ever you are doing a loop to push values it is very important that you use toString() or + "" to the elements you are pushing to an array or object. That forces the code to go back to the server to get the value. I have had cases where the same value is repeated.
Try the following:
var SI_SetAssignedToFields = Class.create();
SI_SetAssignedToFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssignedToFields: function () {
var assignedToTeam = this.getParameter('sysparm_assignedToTeam');
//gs.log('Inside function ', 'CBT_SI_SetAssignedToFields');
var obj = {};
if (assignedToTeam != '') {
gs.log('Team: ' + assignedToTeam, 'SI_SetAssignedToFields');
var grp = new GlideRecord('sys_user_group');
grp.addQuery('sys_id', assignedToTeam);
grp.query();
while (grp.next()) {
obj.department = grp.u_department.toString();
obj.group = grp.u_group.toString();
}
}
else {
obj.department = "";
obj.group = "";
}
gs.log('Dept: ' + obj.department + ', Group: ' + obj.group, 'SI_SetAssignedToFields');
var json = new global.JSON().encode(obj);//JSON formatted string
gs.log('Answer: ' + json, 'SI_SetAssignedToFields');
return json;
},
type: 'SI_SetAssignedToFields'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2017 06:10 AM
Thanks Michael, the toString() did the trick.
I have a follow up question/issue. I'm calling the script include from a client script to set the department and group based on the team selected. Neither the department or the group is displayed on the form; they are being set for backend reporting.
I can see the values for the answer object being set now that I'm using the toString(), but the fields are still not being set. Do you think it's because they are not displayed on the form? Do I need toString() here as well?
g_form.setValue('u_assigned_to_group', answer.group);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2017 07:39 AM
Kimberly, in order for a client script to set a field value, that field must be on the form. Since they are hidden, the script won't work. What you could do is enable the field, set the value and then hide the field all in the same script. Or a better option would be to set these values via business rule instead on insert.