Calling Script Includes variables from other function

MGanon
Tera Guru

How can Script Includes variables be called by other functions?

I have a Script Includes with several functions. Many of the data sources share a similar data source. Many of the functions generate some of the same variables. To avoid duplicating the scripting code required to populate the common variables, I want to write the script once and call that result as needed.

How?

For example,

function1
var A = lines of script to generate the "A" variable
var B = lines of script to generate the "B" variable
var C = lines of script to generate the "C" variable
var X = lines of script to generate

function2
var A = lines of script to generate the "A" variable
var B = lines of script to generate the "B" variable
var C = lines of script to generate the "C" variable
var Y = many lines of script to generate ""

function3
var A = lines of script to generate the "A" variable
var B = lines of script to generate the "B" variable
var C = lines of script to generate the "C" variable
var Z = many lines of script to generate ""

Because variables A, B, and C each require many lines and generate the same output from the same input for each function, I would like to write the code once and call the common variables as needed.

1 ACCEPTED SOLUTION

Uncle Rob
Kilo Patron

In the initialize secition of the script include define your variables A,B,C, but have them generated right then with private functions in the include.  

 

this.A = this.generateA();
this.B = this.generateB();

generateaA = function () {
//do stuff to make A;
//return results;
};

generateaB = function () {
//do stuff to make B;
//return results;
};

otherFunction = function(){
//do stuff using this.A or this.B
}

View solution in original post

12 REPLIES 12

I understand that a Script Include can receive parameters passed when calling the SI function. What I don't get is how to pass those parameters to the private functions within the "initialize: function(){" section at the top. Do I pass the parameters as "initialize: function(sourceParameter){" or to the private functions such as "generateA = function(sourceParameter){" or do I only pass the parameters to the functions outside of the initialize section.

Once inside of the private functions within the initialize section, what is the syntax to return the results? is it "return;" or  "return A;" or  "return this.A;" or something else

After calculating all of the private functions in the initialize section, is there as special syntax to use the this.X variables?

MGanon
Tera Guru

I still don't get when to use "this." or how to call multiple variables created and populated through a single function in the initialize section

I create Utility script includes to do my work for me and I then call them from business rules, etc.for example:

//*****************************//  Script Include//*****************************

Script Include name: myApp_UtilsScript:

var myApp_Utils = Class.create();myApp_Utils.prototype = {    initialize: function()}    }    //example function

    getMyData: function(parm1, parm2){        var rv = '';        var gr = new GlideRecord('sys_user');
        gr.addQuery('name', parm1);        gr.query();        if (gr.next()){            rv = gr.getValue('someField');
        }        return rv;    }}

 

 

//**********************************************
//  to call your method from a business rule for example//**********************************************var utils = new myApp_Utils();var x = utils.getMyDate('1', '2');//now you have x, do what you need to do with it

create Utility script includes to do my work for me and I then call them from business rules, etc.

for example:

//*****************************
//  Script Include
//*****************************

Script Include name: myApp_Utils

Script:

var myApp_Utils = Class.create();

myApp_Utils.prototype = {

    initialize: function(){

    },

     //example function

    getMyData: function(parm1, parm2){

        var rv = '';

        var gr = new GlideRecord('sys_user');
        gr.addQuery('name', parm1); 

        gr.query(); 

        if (gr.next()){ 

            rv = gr.getValue('someField');
        }

        return rv; 

   }

}
 

//**********************************************
//  to call your method from a business rule for example

//**********************************************
var utils = new myApp_Utils();
var x = utils.getMyData('1', '2');

//now you have x, do what you need to do with it


MGanon
Tera Guru

Thank you ggg,
That outlines the solution for the next step that I will need to take but I still don't see how to return multiple variables at the same time when the variables all use the same source data and also rely on previous variables.

I need to set multiple variables 1 time in a Script Includes. How do I store those initialized variables and call them later?

I then need to call several of those variables in other functions within other scripts. The other script could be that same Script Includes but can also be a business rule or another script. Regardless, I need to set multiple variables in a single function.

 

initialize function(){
read source data
create variable1
create variable2
create variable3
return something
}

function1: function(source){
use some or all of the initialized variables in this function script
}

function2: function(source){
use some or all of the initialized variables in this function script
}