How to retrieve multiple values from a script include and pass it on to a client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2019 10:28 AM
Hi,
I have set the due date and the SLA days based on Item field and i need to merge the two script include and two client scripts into one as both are from the same table and the code is the almost the same as well.
Code:
To get the due date and SLA days(Client script):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.addInfoMessage("To get due date client script");
var getSlaValue = "";
if (g_form.getValue('u_state') != 4 && g_form.getValue('u_state') != 5)
{
getSlaValue = "YES";
}
else if (g_form.getControl('u_state').changed && g_form.getValue('u_state') == 4 || g_form.getValue('u_state') == 5 )
{
getSlaValue = "YES";
}
else
{
getSlaValue = "No";
}
if (getSlaValue == "YES")
{
var cat = g_form.getValue('u_category');
var subcat = g_form.getValue('u_sub_category');
var itm = g_form.getValue('u_item');
g_form.setValue("u_due_date","");
var ajax = new GlideAjax('MyDateTimeAjax');
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.addParam('sysparm_cat', cat);
ajax.addParam('sysparm_subcat', subcat);
ajax.addParam('sysparm_itm', itm);
ajax.getXML(getResponse);
}
function getResponse(response)
{
var slaResult = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('u_due_date', slaResult);
}
}
The only change happening to get the SLA days in the other client script is the last function line:
g_form.setValue("u_sla_days",slaResult);
Script Include:
MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor,
{
nowDateTime: function ()
{
gs.log("MyDateTimeAjax");
var nd = new GlideDateTime(gs.nowDateTime());
gs.log("nd value is" + nd);
var cat = this.getParameter('sysparm_cat');
var subcat = this.getParameter('sysparm_subcat');
var itm = this.getParameter('sysparm_itm');
var gr = new GlideRecord("u_sla_admin_details");
gr.addQuery("u_category", cat);
gr.addQuery("u_sub_category", subcat);
gr.addQuery("u_item", itm);
gr.addQuery("u_record_type", "ADMIN");
gr.addQuery("u_active", "true");
gr.query();
if(gr.next())
{
var slaResult = (gr.u_ttr * 1 );
var slaCall = "";
if (gr.u_sla_days != "")
{
slaCall = (gr.u_sla_days * 1);
if(nd.getDayOfWeekUTC() == 6)
{
nd.addDaysLocalTime(2);
}
else if(nd.getDayOfWeekUTC() == 7)
{
nd.addDaysLocalTime(1);
}
nd.addDaysUTC(slaCall);
if(nd.getDayOfWeekUTC() == 6 )
{
nd.addDaysLocalTime(2);
}
else if(nd.getDayOfWeekUTC() == 7)
{
nd.addDaysLocalTime(1);
}
return nd;
}
else
{
slaCall = "Not Applicable";
nd = slaCall;
gs.log("Inside MyDateTimeAjax else part nd value: " + nd);
return nd;
}
}
}
});
var Admin_Get_SLA_Days = Class.create();
Admin_Get_SLA_Days.prototype = Object.extendsObject(AbstractAjaxProcessor, {
sla_days_value:function()
{
var cat = this.getParameter('sysparm_cat');
var subcat = this.getParameter('sysparm_subcat');
var itm = this.getParameter('sysparm_itm');
var gr = new GlideRecord("u_sla_admin_details");
gr.addQuery("u_category", cat);
gr.addQuery("u_sub_category", subcat);
gr.addQuery("u_item", itm);
gr.addQuery("u_record_type", "ADMIN");
gr.addQuery("u_active", "true");
gr.query();
if(gr.next())
{
var slaResult = (gr.u_ttr * 1);
var slaCall = "";
if (gr.sla_days != "")
{
slaCall = (gr.u_sla_days * 1); //1
return (slaCall + " Day(s)");
}
else if (gr.sla_days == "")
{
slaCall = "Not Applicable";
return (slaCall);
}
}
},
});
The bold coded lines are the changes that has to be merged.
Please help me out with this

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2019 10:41 AM
Hi,
My answer will not show you how to change your code, but an approach that you can use to rewrite it.
If you want a function a Script Include to return multiple values you should return an object instead of one value. This object should be converted to a JSON string. This way you can retrieve it in your Client Script. Please see below my example to retrieve multiple user information from a single function. You can then rewrite your code so that it would use the same approach.
This is a script include:
var UserLib = Class.create();
UserLib.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// parameter is used when the script is not called via GlideAjax
getUserData: function(sys_id){
var userSysId = this.getParameter('sys_parm_user_sys_id') ? this.getParameter('sys_parm_user_sys_id') : sys_id; // check what type of parameter was used, by GlideAjax on as an argument.
var user = {}; // object that will be filled in
var grUser = new GlideRecord('sys_user');
grUser.get(userSysId);
// add multiple information to single object
user.name = grUser.getDisplayValue('name');
user.title = grUser.getDisplayValue('title');
user.email = grUser.getDisplayValue('email');
user.lastLoginTime = grUser.getDisplayValue('last_login_time');
var lastLogin = new GlideDateTime(user.lastLoginTime);
var current = new GlideDateTime(gs.nowDateTime());
user.isValid = lastLogin.compareTo(current); // -1 means that last login is before current date, 0 dates are equal, 1 menas after
// return JSON string of the object
return JSON.stringify(user);
},
type: 'UserLib'
});
And this how I use it in a Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('global.UserLib');
ga.addParam('sysparm_name','getUserData');
ga.addParam('sys_parm_user_sys_id',newValue);
ga.getXML(getData);
}
function getData(response){
// parse the answer to an object
var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
// check if answer has been set correctcly
if (typeof answer != 'undefined') {
if(answer.isValid == -1){
g_form.setValue('u_tite',answer.title);
g_form.setValue('u_email',answer.email);
}
}
}
If my answer helped you in any way, please then mark it as helpful. If this solved your case please mark it as a correct answer. Thank You.
Best regards,
Łukasz