- 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:36 AM
Hi,
please refer bellow code
Client script
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
var v=[];
var a=answer.split(',');
v.push(a[0]);
v.push(a[1]);
//v = JSON.parse(answer);
//alert(v[0]+" "+v[1]);
g_form.setValue('u_manager',v[1]);
g_form.setValue('u_email',v[1]);
}
Script include
please see this is sample script how the data has been returned from SI
var GettingManager = Class.create();
GettingManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
AutoPopulateManager : function(){
var gb = this.getParameter('sysparm_user_name');
var gc =[];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',gb);
gr.query();
if(gr.next()){
gc.push(gr.manager.getDisplayValue());
gc.push(gr.email);
return gc.toString();
}},
type: 'GettingManager'
});
if any query reply same
varsha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2019 12:02 PM
Hi Varsha,
thanks for the response. By doing it your way with the scrips in your post, does the order of the array need to stay in a particular order? The advantage of the JSON method would be to not have to keep the returned values in a specific order. thoughts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2019 12:17 PM
yes,
order is important in array.
i don't have more idea about json.
thank you
varsha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2019 12:41 PM