What is Script Include?
Script includes are used to store JavaScript that runs on the server. Script Includes are reusable server-side script logic that define a function or class. Script Includes execute their script logic on Server Side only when explicitly called by other scripts whether from Server side or client side. Consider using script includes instead of global business rules because script includes are only loaded on request.
There are different types of Script Includes:
On demand/classless
Extend an existing class
Define a new class
On Demand/ classless Script Include - A Script Include that defines a single function is known as an on demand, or classless Script Include. The function is callable from other server-side scripts. On demand Script Includes can never be used client-side even if the Client callable option is selected. The Script Include name must exactly match the name of the function. On demand Script Includes are typically used when script logic needs to be reused. Examples include standardizing date formats, enabling/disabling logging, and validating email addresses.
For Example, Script Include Name & function name will be same.
Script Include Name : getEmailId
function : getEmailId() {
var user = gs.getUserId();
var userRecord = new GlideRecord('sys_user');
userRecord.get(user);
if(userRecord) {
return userRecord.getDisplayValue('email');
} }
Define a new Class Script Include - Utilities Script Includes typically define a new class and therefore use the automatically inserted script template. The initialize function is automatically invoked when JavaScript objects are instantiated from the Script Include. Any variable defined as part of the this object in the initialize function is known to all other functions in the Script Include.
ScriptIncludeName :getSpecificLocation
var getSpecificLocation = Class.create();
getSpecificLocation.prototype = {
initialize: function() { },
function : getSpecificLocation() {
var grLoc = new GlideRecord('cmn_location');
grLoc.addQuery('country', 'India');
grLoc.query();
while(grLoc.next()) {
return grLoc.location + ''; }
type: 'getSpecificLocation' };
Extended Script Include- Script Includes can extend existing Script Includes by adding new methods and non-method properties. Although most ServiceNow classes are extensible, the most commonly extended classes are: GlideAjax:
make AJAX calls from Client Scripts
LDAPUtils: add managers to users, set group membership,
debug LDAP Catalog*: set of classes used by the Service Catalog for form processing and UI building
Let's try to understand that How to call Server side script through client side script in ServiceNow?
Example : How to populate the email id of requester for in any of catalog item using ScriptInclude & GlideAjax ?
ScriptInclude :getDetails
var getDetails = Class.create();
getDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getValue : function() {
var email_id;
var uid =this.getParameter('sysparm_requested_for');
var getUserD = new GlideRecord('sys_user');
getUserD.addQuery('sys_id',uid);
getUserD.query();
if(getUserD.next()) {
return getUserD.email;
} },
type: 'getDetails'
});
Catalog onchange client script :getData
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return; }
var ga = new GlideAjax('getDetails');
ga.addParam('sysparm_name','getValue');
ga.addParam('sysparm_requested_for',g_form.getValue('requested_for'));
ga.getXML(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('email_id',answer);
} }
This comment has been removed by the author.
ReplyDeletethanks guys for liking my blogs !
ReplyDelete