How to retrieve multiple values from a script include and pass it on to a client script (2024)

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

How to retrieve multiple values from a script include and pass it on to a client script (2024)

FAQs

How to send more than one variable from server-side script to client side while using GlideAjax ServiceNow? ›

Server side
  1. Prepare the data you want to send to the client. ...
  2. Instantiate a JSON object and encode the data to be sent.
  3. Return the data. ...
  4. Retrieve the data.
  5. Decode the JSON formatted string into an object or array, which contains multiple values.
  6. Process the values regarding the business requirements.
Nov 17, 2015

What is the use of getDisplayValue in ServiceNow? ›

getDisplayValue() ) gives you the value that is displayed for the field's value. You might think a field's value is what it is, but certain field types can have a disparity between the actual value, and the display value. For example, drop-down fields can have a label as well as a value.

What is Glide Ajax in ServiceNow? ›

GlideAjax class allows the execution of server-side code from the client. GlideAjax calls pass parameters to the script includes, and, using naming conventions, allows the use of these parameters. if you want to call GlideRecord in client scripts use GlideAjax to reduce perfomance issue.

How do I return multiple values in return statement? ›

To return multiple values from a function in C, you can either use an array, a structure, or pointers. You can store the values you want to return in an array or structure, and then return its reference (memory address) or modify the values directly using pointers passed as function parameters.

What is the best way to return multiple values in JavaScript? ›

JavaScript does not return multiple values just by calling the function therefore it can be achieved using arrays or objects. To return multiple values from a function using arrays, create an array of the variables that are to be returned and then call the array.

How to assign multiple values to a single variable in shell script? ›

Using an Array

Another way to assign multiple variables using a command's output is to assign the command output fields to an array. In the example, we used the Bash built-in readarray command to read the date command's output. The default delimiter used by the readarray command is a newline character.

How to pass JavaScript variable to server-side? ›

If you need the Javascript variable to go through any logic on the server, then checkout LiveView Javascript Hooks, which work very well and you can create GenServer-like LiveControllers that react to client events. Most of the boilerplate is already setup in the included LiveView javascript.

What is the easy difference between client-side scripting and server-side scripting? ›

The server-side scripting has complete access to all the files present in any web server. The client-side scripting has no access to the files that exist in a web server. Languages like Ruby on Rails, Perl, ASP, Python, ColdFusion, PHP, etc., come into play in the case of server-side scripting.

What is the difference between getDisplayValue and getValue in ServiceNow? ›

getDisplayValue returns the value as you see in the screen, therefore always a string, while getValue returns the value underneath, therefore an object. Which may be a string if the range has text in it. The difference is more clear if the range has numbers or dates in it.

What is the difference between setDisplay and setVisible in ServiceNow? ›

I was asked, what is the difference between setDisplay and setVisible method. I said, setVisible maintains space even if field is hidden and with setDisplay, we won't have space in between the hidden fields.

What is the use of gsftSubmit in ServiceNow? ›

gsftSubmit() is mostly used in UI actions that have a client side and a server side script. At the end of the client side script, gsftSubmit(null, g_form. getFormElement(), "Action name") triggers the UI Action again which is specified in the 3rd parameter, this time only the server side code gets executed.

What is the difference between GlideAjax and GlideRecord in ServiceNow? ›

However, GlideAjax is complex and requires creation of both a server-side and client-side script, so in many cases it's just fine to use another asynchronous method (such as GlideRecord ) to retrieve data from the server, when we need to access related records in a Client Script, UI Policy script, or other client-side ...

What is G_scratchpad? ›

The g_scratchpad object used for passing information from the server to the client when the client requires information not available on a form. This can be accomplished by creating a business rule to put the information in the g_scratchpad object and accessing the information in a client script.

How to return multiple values from a GlideAjax with JSON? ›

  1. Prepare the data you want to send to the client. ...
  2. Instantiate a JSON object and encode the data to be sent.
  3. Return the data. ...
  4. Retrieve the data.
  5. Decode the JSON formatted string into an object or array, which contains multiple values.
  6. Process the values regarding the business requirements.
Nov 17, 2015

How to pass value from client script to processing script in ServiceNow? ›

To pass variable to processing script, similarly to client script, you will need a hidden field but for processing script, you just use the name attribute of input field as variable name. Note that input elements should be placed inside g:ui_form so that its values can be sent to server as form data.

How to return multiple things from a function in JavaScript? ›

The approach of using an array to return multiple values from a JavaScript function involves organizing the values into an array and then utilizing array destructuring to extract and assign those values to individual variables. Example: This example returns the array [“GFG_1”, “GFG_2”] containing multiple values.

Can a function return multiple values in VB Script? ›

In VBScript, the values are returned from a function using function name. In case if you want to return two or more values, then the function name is returned with an array of values.

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6653

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.