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

patricklatella
Mega Sage

Excellent, great to know and thanks again Jim!

Here are my final scripts if you want to copy and paste them back one more time into a response I'll mark your reply as correct.

 

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.company_name = "";
result.email = "";

var user = new GlideRecord('sys_user');
if (user.get(this.getParameter("sysparm_user"))){
result.company_id = user.getValue("company");
result.company_name = user.company.getDisplayValue();

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, result.company_name);
g_form.setValue('email', result.email);
}
}

patricklatella
Mega Sage

actually ignore that last post Jim, I just marked your previous response as the correct answer.  again I appreciate the help!

You are welcome!