- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-12-2021 07:04 AM
Hello All,
here some important point related to the script include :-
Script includes:-
1.Script includes are used to JavaScript that run on server side.
2. it store JavaScript Function and Class.
3.it used to reuse the function and class.
4. It can be reduced line of Code and time.
5. Script Include used business Rule, Reference Qualifier,UI action and in other script Include.
Three Types Of Script Include:-
1)OnDemand/Classless
2)Extending (an existing class)
3)Define a new Class.
1)OnDemand Script Include:-
1. OnDemand script Include are not callable on client side even if the client callable option is select.
2. In OnDemand Script include we cannot write class, only function is return.;
3. If a second function is include in the script Include ,it is only available for use after the first function has executed.
#Syntax of OnDemand Script:-
Function OnDemand()
{
//writing the code
}
In this Script name and Function must be same.
1.onDemand / classless script include:-
#example of :- compare two string are equal or not?
script include file:
/* function Name:-onDemandDemo
@parameter:- string1,string2
@description: checked two string are equal or not .
*/
function onDemandDemo(string1,string2){
if(string1 == string2){
return "Matching strings" ;
}
else{
return "string do not Match" ;
}
}
business rule file:- // called script include in business rule.
gs.addInfoMessage(onDemandDemo('priya','priya'));
2)Define a new Class:-
1.Define a new class means it create a new class in this class we can write multiple function.
2. Initialize function is automatically called when javascript object are created.
3.Script include can be Public or private. Most client callable script include are marked private by default. Also important to note that Script include is execute only when explicitly called by other scripts
Syntax:-
var Classdemo = Class.create();
Classdemo.prototype = {
initialize: function() {},
type: 'Classdemo' };
example 1 :- After creation of incident create a problem and change related of that incident.
//asignment 5 - name of script include
var assignment5 = Class.create();
assignment5.prototype = {
checkFunction : function()
{
var short_description=current.short_description;
var gr=new GlideRecord('problem');
var gc=new GlideRecord('change_request');
gc.short_description=short_description;
gr.short_desrcription=short_description;
gr.insert();
gc.insert();
gs.addInfoMessage("Problem Number"+gr.number+"Change Request Number is"+gc.number+"Has created");
},
type: 'assignment5'
};
/*Here called the script include in business rule*/
business rule file:
(function executeRule(current, previous /*null when async*/) {
var x=new assignment5();
x.checkFunction ();
})(current, previous);
3)Extend an existing class:-
1. We can call this function on client side.
2. We choose client callable then using Glide Ajax we call server side Function on client side.
3.gs.include(‘Script_name’): These include method call another script include to user current script.
Syntax:-
var Classdemo = Class.create(); Classdemo.prototype = Object.extendsObject(AbstractAjaxProcessor, { type: 'Classdemo' });
Question:-1. Populate the manager's name of the selected user.
/* script include file */
@assignment4:- name of script include file
var assignment4 = Class.create();
assignment4.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkValue: function() {
var caller = this.getParameter('sysparm_caller');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', caller);
gr.query();
if (gr.next()) {
var mangerName = "Manger is== "+gr.manager.getDisplayValue();
}
return mangerName;
},
});
//client script file ::-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga=new GlideAjax('assignment4');
ga.addParam('sysparm_name','checkValue');
ga.addParam('sysparm_caller', g_form.getValue('caller_id'));
ga.getXML(callbackfunction);
function callbackfunction(response)
{
var answer=response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
//please Mark it helpful or Bookmark it for your reference.
Regards,
priyanka
- 3,847 Views