how to pass value from function to another in script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2016 11:58 PM
Hi all ,
I am trying to fetch a value from one function to another,
here is my code
var CatalogDynamicValueTest = Class.create();
//var blueId={};
CatalogDynamicValueTest.prototype = {
initialize: function() {
},
//To Get Cloud Type
getCloudTypeValues: function() {
gs.log("Inside Get Cloud Type");
var tenCatName;
var tenCatVersion;
var catItem=current.variables.TenantCatalog;
gs.log("New Selected Catitem:"+ catItem);
var grTenCtg = new GlideRecord('u_compose_tenant_catalog');
grTenCtg.addQuery('sys_id',catItem);
grTenCtg.query();
while(grTenCtg.next())
{
tenCatName = grTenCtg.u_symbolic_name;
tenCatVersion = grTenCtg.u_version;
gs.log("catTENCATVERSION: " + tenCatVersion);
gs.log("Ten Name: " + tenCatName);
}
var blueprintId;
var grBlueprint = new GlideRecord('u_compose_blueprint');
grBlueprint.addQuery('u_blueprint_name',tenCatName);
grBlueprint.addQuery('u_version',tenCatVersion);
grBlueprint.query();
while(grBlueprint.next())
{
gs.log("TENCATVERSION: " + tenCatVersion);
gs.log("Catalog Id: " + grBlueprint.u_id);
blueprintId = grBlueprint.u_id;
}
/*g_scratchpad.saved_blueprintId=blueprintId;
var abc =g_scratchpad.saved_blueprintId;
gs.log("Stracth pd" +abc);*/
gs.log("newblueprintId: " +blueprintglobal);
var arrTarCId = ' ';
var grCTC = new GlideRecord('u_compose_catalog_target_cloud');
grCTC.addQuery('u_blueprint_id',blueprintId);
grCTC.query();
while(grCTC.next())
{
//arrTarCId.push(grCTC.u_target_cloud_id);
gs.log("gsCTC Target Cloud id :" + grCTC.u_target_cloud_id);
if(arrTarCId.length>0)
arrTarCId +=("," + grCTC.u_target_cloud_id);
//gp += (gr.u_cloud_code + ",");
else
arrTarCId= grCTC.u_target_cloud_id;
}
gs.log("Array of Target: " +arrTarCId);
var arr= [];
arr= arrTarCId.split(",");
var arrTarid=[];
for (var n=1; n <arr.length; n++)
{
arrTarid.push(arr[n]);
gs.log(" Target Ref:" + arr[n]);
}
gs.log("Target Final ar" + arrTarid);
//Displaying Logs
// for(var ii=0;ii<arrTarCId.length;ii=ii+1)
// {
// gs.log("Array" + arrTarCId[ii]);
// }
gs.log("Array Length: " + arrTarid.length);
var arrayUtil = new ArrayUtil();
var arrCCId = [];
var arrCC = [];
var arruniqueCC=[];
for(var i=0;i<arrTarid.length;i=i+1)
{
gs.log("Array1" + arrTarid[i]);
var val = arrTarid[i];
var grTCC = new GlideRecord('u_compose_target_cloud_category');
grTCC.addQuery('u_id',val);
grTCC.query();
while(grTCC.next())
{
arrCCId.push(grTCC.u_cloud_category_id);
arrCC.push(grTCC.u_cloud_code);
//gs.log("grTCC Cloud Id: " + grTCC.u_cloud_category_id);
gs.log("grTCC Cloud Code: " + grTCC.u_cloud_code);
}
}
// arruniqueCC.push(arrayUtil.unique(arrCC));
gs.log("Cloud CC: " + arrCC.length);
//gs.log("Array Unique CC:" + arruniqueCC);
//gs.log("Array Unique Length: " + arruniqueCC.length);
var arrTLCC = [];
for(var j=0;j<arrCC.length;j=j+1)
{
var grTenLoc = new GlideRecord('u_compose_tenent_location');
grTenLoc.addQuery('u_target_cloud_code',arrCC[j]);
grTenLoc.addQuery('u_endpoint',current.variables.Endpoint);
grTenLoc.query();
gs.log("Inside Ten:"+ arrCC[j]);
while(grTenLoc.next())
{
gs.log("While Ten:");
arrTLCC.push(grTenLoc.u_target_cloud_code);
gs.log("Tentant Cloud Code : " + grTenLoc.u_target_cloud_code);
}
}
gs.log("Ten Cloud CC: " + arrTLCC.length);
var arrTCCid=[];
for(var k=0;k<arrTLCC.length;k=k+1)
{
var grTCCone = new GlideRecord('u_compose_target_cloud_category');
grTCCone.addQuery('u_cloud_code',arrTLCC[k]);
grTCCone.query();
while(grTCCone.next())
{
gs.log("Cloud Category Id: " + grTCCone.u_cloud_category_id);
arrTCCid.push(grTCCone.u_cloud_category_id);
}
}
gs.log("Cloud Category id length: " + arrTCCid.length);
var gp= ' ';
for (var l=0;l<arrTCCid.length;l=l+1)
{
var grcc=new GlideRecord('u_compose_cloud_category');
grcc.addQuery('u_id',arrTCCid[l]);
//gs.log("Tc For entry");
grcc.query();
while(grcc.next())
{
if (gp.length > 0) {
//build a comma separated string of groups if there is more than one
gp += (',' + grcc.u_category_code);
}
else {
gp = grp.u_category_code;
}
//gs.addInfoMessage("Cloud Category "+ gp);
// gs.log("Cloud Category" + grcc.u_category_name);
//gs.log("Gp :" + gp);
}
}
return 'u_category_codeIN' + gp;
},
This a function and i want fetch the value(blueprint which is highlighted in red) in next function
getOSValues: function() {
var getid=getCloudTypeValues().blueprintId;
gs.log("Blueprint id cheeck"+ getid);
//gs.log("blueID"+blueprintglobal.getId);
//gs.log("Inside Os");
var os ='';
var sel=current.variables.Location.u_target_cloud_code;
// gs.log("Selection" + sel);
var targetcode=sel;
gs.log("Target Code:" + targetcode);
/* var gr= new GlideRecord('u_compose_tenant_location');
gr.addQuery('sys_id',sel);
gr.query();
while(gr.next())
{
targetcode=gr.u_target_cloud_code;
}*/
var grtcc= new GlideRecord('u_compose_target_cloud_category');
grtcc.addQuery('u_cloud_code',targetcode);
grtcc.query();
while(grtcc.next()){
gs.log("while os");
gs.log("OS Final res" + grtcc.u_id);
if(os.length>0)
os +=("," + grtcc.u_id);
//gp += (gr.u_cloud_code + ",");
else
os=grtcc.u_id;
}
var arr= [];
gs.log("os: " + os);
arr= os.split(",");
var arrOS=[];
for (var i=0; i <arr.length; i++)
{
arrOS.push(arr[i]);
gs.log(" Os Ref:" + arr[i]);
}
gs.log("osFinal ar" + arrOS);
var osid= ' ';
var blue= getCloudTypeValues.blueprintglobal;
//var idrt = blueprintglobal.getId;
gs.log("Inside blue" + blue);
for(var j=0; j<arrOS.length; j++)
{
gs.log("Os: " + arrOS[j]);
var grtcos= new GlideRecord('u_compose_catalog_target_cloud');
grtcos.addQuery('u_target_cloud_id',arrOS[j]);
//grtcos.addQuery('u_blueprint_id',blueprint);
grtcos.query();
while(grtcos.next())
{
gs.log("Operating ids:" + grtcos.u_operating_system_id);
if(osid.length>0)
osid +=("," + grtcos.u_operating_system_id);
//gp += (gr.u_cloud_code + ",");
else
osid =grtcos.u_operating_system_id;
}
}
var arrosid= [];
arrosid= osid.split(",");
var arrOSid=[];
for (var k=1; k<arrosid.length; k++)
{
arrOSid.push(arrosid[k]);
gs.log("Operating Ref:" + arrosid[k]);
}
gs.log("os id:" + arrOSid);
var tl= ' ';
for(var l=0;l<arrosid.length;l++){
gs.log("Msgg: " + arrosid[l]);
var grtl = new GlideRecord('u_compose_operating_system');
grtl.addQuery('u_id',arrosid[l]);
grtl.query();
while(grtl.next()){
gs.log("Mess: " + grtl.u_display_name);
if(tl.length>0)
tl +=("," + grtl.u_display_name);
else
tl = grtl.u_display_name;
}
}
gs.log("TL: " + tl);
return 'u_display_nameIN' + tl;
},
highlighted part is in second function.
Am I following correct method to fetch?
Pleas help me to get solution.
Thanks
Pradeep

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2016 12:03 AM
Hi Pradeep,
In your initialize function, declare your variable there, similar to below.
initialize: function() {
this.blueprintId = null;
},
In your function where you are setting the value, use the following.
this.blueprintId = grBlueprint.u_id;
Now you can access the value you stored anywhere else in the script just by calling this.blueprintId;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2016 09:30 PM
Hi Amann,
Could you lease help me to understand why in other function its taking null value.
initialize: function() {
this.blueprintId = null;
},
getCloudTypeValues: function() {
this.blueprintId= grBlueprint.u_id;// here we get values
return(I am returning different value other than blueprintId here);
},
getLocationValues: function() {
//here I want to access the blueprintId
gs.log(this.blueprintId);// but here i get null value
},
How to access that value in getLocationValues function.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2016 12:06 AM
Hi Pradeep,
Where these methods are located? In the same script include?? If answer is Yes then you can use 'this' object to call other method in your first method.
And if it is located in other script include then you can use below syntax
var sec_method=new other_script_include().method_name(<pass parameters>);
Thanks,
Abhinandan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2016 12:24 AM
Hi Abhinandan,
Yes all function are in same script include
So can I use like this?
var getid=this.getCloudTypeValues().blueprintId;