Help with JSON().encode

kimberlylp
Giga Guru

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.

                                               
           
Script Include:
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;
obj.group = grp.u_group;
}
}
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'
});
1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

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'


});


View solution in original post

7 REPLIES 7

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

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'


});


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_dept', answer.department);


  g_form.setValue('u_assigned_to_group', answer.group);


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.