- 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 11:26 AM
Hello,
Can you try to add JSON object to Array and encode that array variable ?
--
for example :
getAssignedToFields: function () {
var assignedToTeam = this.getParameter('sysparm_assignedToTeam');
//gs.log('Inside function ', 'CBT_SI_SetAssignedToFields');
var obj = {}, ary = [];
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;
obj.group = grp.u_group;
ary.push(obj);
}
}
else {
obj.department = "";
obj.group = "";
ary.push(obj);
}
gs.log('Dept: ' + obj.department + ', Group: ' + obj.group, 'SI_SetAssignedToFields');
var json = new global.JSON().encode(ary);//JSON formatted string
gs.log('Answer: ' + json, 'SI_SetAssignedToFields');
return json;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2017 11:27 AM
Also, can you share how are you accessing ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2017 11:34 AM
Thanks for your time Chirag.
No joy, it just added brackets to the json variable.
Information | Answer: [{"department":{},"group":{}}] |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2017 11:38 AM
Thanks,
Can you try using JSON.stringify() and parse() functions? for example:
var Testing = Class.create();
Testing.prototype = {
initialize: function() {
},
getTestData : function () {
var obj = {};
obj.first_name = 'test';
return JSON.stringify(obj);
},
type: 'Testing'
};
var obj = JSON.parse(new global.Testing().getTestData());
gs.print(obj.first_name);