JSON.parse() not working in Service Portal?

patricklatella
Mega Sage

Hi all, 

seeing several posts about different ways to parse a JSON in a client script to populate fields on a catalog item in Service Portal.  Not sure why my script below isn't working, anyone see what I'm missing?  thanks!

Script Include:

var u_Ajax_User_Info = Class.create();
u_Ajax_User_Info.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserInfo: function(){

var user = this.getParameter('sysparm_user');//value passed from client script
var userInfo = new GlideRecord('sys_user');//table where desired variable lives
userInfo.addQuery('sys_id',user);//field of desired variable on table
userInfo.query();

if(userInfo.next()){
var obj = {};
obj.var1 = userInfo.company;
obj.var2 = userInfo.email;
var json = new JSON();
var data = json.encode(obj);//JSON formatted string
return data;
}
},
type: 'u_Ajax_User_Info'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (/*isLoading ||*/ newValue == '') {
return;
}

var getUser = new GlideAjax('u_Ajax_User_Info');
getUser.addParam('sysparm_name', 'getUserInfo');
getUser.addParam('sysparm_user',g_form.getValue('requested_for'));
getUser.getXML(setValues);
}

function setValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer); //Transform the JSON string to an object
g_form.setValue('business_unit',answer.var1);//field on form
g_form.setValue('email',answer.var2);//field on form
}

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

Modified Script Include:

var u_Ajax_User_Info = Class.create();
u_Ajax_User_Info.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  getUserInfo: function(){
    var result = {};
    result.company_id = "";  //uses descriptive names
    result.company_name = "";
    result.email = "";

    var gr = new GlideRecord('sys_user');//table where desired variable lives
    if (gr.get(this.getParameter("sysparm_user"))){
      result.company_id = gr.getValue("company");
      result.company_name = gr.company.getDisplayValue();
      result.email = gr.getValue("email");
    }
    return JSON.stringify(result);  //return the results as a JSON formatted string
  },

  type: 'u_Ajax_User_Info'
});

 

And the Client Script:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) {
    return;
  }

  if (newValue == ""){
    //clear any extra information
    g_form.setValue("business_unit", "");
    g_form.setValue("email", "");
    return;
  }

  var ga = new GlideAjax("u_Ajax_User_Info");
  ga.addParam("sysparm_name", "getUserInfo");
  ga.addParam("sysparm_user", newValue);  //assuming Client Script is on the requested_for field
  ga.getXMLAnswer(setValues);
}

function setValues(response) { 
  result = JSON.parse(response); //Transform the JSON string to an object 
  g_form.setValue('business_unit', result.company_id, result.company_name);  //assuming this is a reference field
  g_form.setValue('email', result.email);
}

The scripts are not tested but I think they are all OK.  Using a 3rd parameter for setting the "business_unit" field will avoid another trip to the server to get the display value.

View solution in original post

17 REPLIES 17

ahh...missed that edit in the script...let me try it now

sorry, yes your script works...missed a couple things.  this is now working, Excellent, thanks again Jim!  very elegant.

 

SCRIPT INCLUDE:

var u_Ajax_User_Info = Class.create();
u_Ajax_User_Info.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserInfo: function(){
var result = {};
result.company_id = ""; 
result.email = "";

var user = new GlideRecord('sys_user');//table where desired variable lives
if (user.get(this.getParameter("sysparm_user"))){
result.company_id = user.getValue("company");
result.email = user.getValue("email");
}
return JSON.stringify(result); //return the results as a JSON formatted string
},

type: 'u_Ajax_User_Info'
});

 

CLIENT SCRIPT:

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}

//call script include to retrieve values from the [requested_for] user record

var getUser = new GlideAjax('u_Ajax_User_Info');
getUser.addParam('sysparm_name', 'getUserInfo');
getUser.addParam('sysparm_user', newValue);
getUser.getXMLAnswer(setValues);

 

function setValues(response) {
result = JSON.parse(response); //Transform the JSON string to an object
g_form.setValue('business_unit', result.company_id); 
g_form.setValue('email', result.email);
}
}

Thanks, glad that helped you out.

Is the "business_unit" variable a reference to the Company table?  If so, you should include both the sys_id and the display value of the Company in the returned JSON string and use them both in the setValue() call.  This will avoid another trip to the server to get the display value for that record.  That's one of the benefits of returning multiple values in a GlideAjax call.

You'll see I had that in my initial response and a link to the setValue() method using a 3rd parameter (setValue("field_name", sysId, displayName))

I see, ok I've added that.  So that is best practice when returning a value for any reference field using this JSON method?  So if I add other reference field values to this JSON I should do the same for those too?

Yeah, exactly.  Otherwise, when you use "setValue(field_name, sys_id)", the platform has to go back to the server to get the display value for it.  So seeing as you are already getting the sys_id, grab the display value as well and use it as the 3rd parameter.

A lot of people will skip that 3rd parameter because they don't know about it.