- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2019 11:09 AM
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
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2019 12:34 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2019 11:42 AM
company is the reference field on user form so it will not give value using userInfo.company;
you should use like
userInfo.company.getDisplayValue();
Note: also check the email and company is there or not for user.also i have not used JSON anywhere.
varsha.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2019 01:53 PM
Hi Varsha,
thanks again for your help. I was able to get the solution working with both your array solution and the JSON solution I just posted. Here is the solution that works for the array solution. I appreciate your help!
SCRIPT INCLUDE
var u_Ajax_User_Info = Class.create();
u_Ajax_User_Info.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function(){
var obj = [];
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();
// Query user records
if(userInfo.next()){
obj.push(userInfo.company);
obj.push(userInfo.email);
return obj.toString();
},
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.getXML(setValues);
function setValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var v=[];
var a=answer.split(',');
v.push(a[0]);
v.push(a[1]);
g_form.setValue('business_unit',v[0]);
g_form.setValue('email',v[1]);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2019 12:34 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2019 01:42 PM
Hi Jim,
thanks for that script...I needed to add a line and edit another;
var answer = response.responseXML.documentElement.getAttribute("answer");
result = JSON.parse(answer); //Transform the JSON string to an object
but this is now working...thanks! Jim if you want to copy and paste these scripts in another reply I'll mark your answer correct so it's in one place.
CLIENT SCRIPT:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var getUser = new GlideAjax('u_Ajax_User_Info');
getUser.addParam('sysparm_name', 'getUserInfo');
getUser.addParam('sysparm_user', newValue);
getUser.getXML(setValues);
function setValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
result = JSON.parse(answer); //Transform the JSON string to an object
g_form.setValue('business_unit', result.company_id);
g_form.setValue('email', result.email);
}
}
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 gr = new GlideRecord('sys_user');
if (gr.get(this.getParameter("sysparm_user"))){
result.company_id = gr.getValue("company");
result.email = gr.getValue("email");
}
return JSON.stringify(result); //return the results as a JSON formatted string
},
type: 'u_Ajax_User_Info'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2019 01:50 PM
You shouldn't have to - the "getXMLAnswer()" method will return just the contents of the answer node for you automagically.
Did you happen to try the script as it was?