The CreatorCon Call for Content is officially open! Get started here.

How to create variables from array's?

CPKIII
Tera Expert

I am sure I am missing something simple here.   I have a query to a table that looks like below.

var sys = "87b4b72c3869310001a8fa55d9ddeb4b";

var bud = new GlideRecord('u_demand_budget');

bud.addQuery('u_demand.sys_id',sys);

bud.query();

while(bud.next()){

var list = bud.u_fiscal_year.toString();

var fy = list.split("|");

for (var i=0; i < fy.length ; i++) {

gs.print(fy[i]);

}

}

The Print looks like this.  

*** Script: FY17

*** Script: FY18

*** Script: FY18

*** Script: FY16

How to get each return to be it's own variable.   IE:

var fy[1] = FY17

var fy[2] = FY18

Again I am sure there is something simple I am missing.   Any help is appreciated.  

1 ACCEPTED SOLUTION

First of all, you need to loop through the whole array like you are doing when you print out the array. Secondly, the parseFloat won't parse past the commas because they are special characters. So it parses up to the special character, like the quote said that I sent you before. So you have to take those out. Here is what the code should look like:



for (var i=0; i < opex.length; i++) {


gs.print("Opex " + opex[i]);


topex += parseFloat(opex[i].replace(/,/g, '').substr(1));


}



All of this is assuming that the string will come through in the same way each time: with a $ at the front and commas in it.


And it would be best to declare topex at the top of your script, like you have done with your other variables.



That should do it! Will you let me know how it worked?


View solution in original post

10 REPLIES 10

Casey thank you for your help.   I am very close to desired output, but have one more small issue.       Script:



var net = [];


var opex = [];


var sys = "87b4b72c3869310001a8fa55d9ddeb4b"


var bud = new GlideRecord('u_demand_budget');


bud.addQuery('u_demand.sys_id',sys);


bud.query();


while(bud.next()){


opex.push(bud.u_total_operating_expenses.getReferenceDisplayValue() + '');


}


for (var i=0; i < opex.length; i++) {


gs.print("Opex " + opex[i]);


}


topex = parseFloat(opex[0].substr(1)) + parseFloat(opex[1].substr(1)) + parseFloat(opex[2].substr(1)) + parseFloat(opex[3].substr(1));


gs.print("Total Opex " + topex);



OUTPUT:


*** Script: Opex $1,000,000.00


*** Script: Opex $0.00


*** Script: Opex $0.00


*** Script: Opex $5,096,350.00


*** Script: Total Opex 6



As you can see it is only adding the first 2 numbers of the Opex Output together.   What needs to be done to make it add the complete number?


First of all, you need to loop through the whole array like you are doing when you print out the array. Secondly, the parseFloat won't parse past the commas because they are special characters. So it parses up to the special character, like the quote said that I sent you before. So you have to take those out. Here is what the code should look like:



for (var i=0; i < opex.length; i++) {


gs.print("Opex " + opex[i]);


topex += parseFloat(opex[i].replace(/,/g, '').substr(1));


}



All of this is assuming that the string will come through in the same way each time: with a $ at the front and commas in it.


And it would be best to declare topex at the top of your script, like you have done with your other variables.



That should do it! Will you let me know how it worked?


Thank you so much for your help.     I posted the final code to the main thread.  



Have a wonderful day.  


CPKIII
Tera Expert

The final product..   I can not thank you enough.


var net = [];


var opex = [];


var am = [];


var ms = [];


var hr = [];


var sf = [];


var co = [];


var cp = [];


var sys = "87b4b72c3869310001a8fa55d9ddeb4b"


var bud = new GlideRecord('u_demand_budget');


bud.addQuery('u_demand.sys_id',sys);


bud.query();


while(bud.next()){


opex.push(bud.u_total_operating_expenses.getReferenceDisplayValue() + '');


  net.push(bud.u_net_operating_expenses.getReferenceDisplayValue() + '');


  am.push(bud.u_amoritization.getReferenceDisplayValue() + '');


  ms.push(bud.u_misc_expense_retire_savings.getReferenceDisplayValue() + '');


  hr.push(bud.u_hardware.getReferenceDisplayValue() + '');


  sf.push(bud.u_software.getReferenceDisplayValue() + '');


  co.push(bud.u_consulting.getReferenceDisplayValue() + '');


  //tco.push(bud.u_misc_expense_retire_savings.getReferenceDisplayValue() + '');


  cp.push(bud.u_total_capital_expenses.getReferenceDisplayValue() + '');



}


for (var i=0; i < opex.length; i++){


}


for (var i=0; i < net.length; i++) {


}


for (var i=0; i < am.length; i++) {


}


for (var i=0; i < ms.length; i++) {


}


for (var i=0; i < hr.length; i++) {


}


for (var i=0; i < sf.length; i++) {


}


for (var i=0; i < co.length; i++) {


}


//for (var i=0; i < tco.length; i++) {


//}


for (var i=0; i < cp.length; i++) {


}






var topex = parseFloat(opex[0].replace(/,/g, '').substr(1)) + parseFloat(opex[1].replace(/,/g, '').substr(1)) + parseFloat(opex[2].replace(/,/g, '').substr(1)) + parseFloat(opex[3].replace(/,/g, '').substr(1));


var neto = parseFloat(net[0].replace(/,/g, '').substr(1)) + parseFloat(net[1].replace(/,/g, '').substr(1)) + parseFloat(net[2].replace(/,/g, '').substr(1)) + parseFloat(net[3].replace(/,/g, '').substr(1));


var amz = parseFloat(am[0].replace(/,/g, '').substr(1)) + parseFloat(am[1].replace(/,/g, '').substr(1)) + parseFloat(am[2].replace(/,/g, '').substr(1)) + parseFloat(am[3].replace(/,/g, '').substr(1));


var mi = parseFloat(ms[0].replace(/,/g, '').substr(1)) + parseFloat(ms[1].replace(/,/g, '').substr(1)) + parseFloat(ms[2].replace(/,/g, '').substr(1)) + parseFloat(ms[3].replace(/,/g, '').substr(1));


var hd = parseFloat(hr[0].replace(/,/g, '').substr(1)) + parseFloat(hr[1].replace(/,/g, '').substr(1)) + parseFloat(hr[2].replace(/,/g, '').substr(1)) + parseFloat(hr[3].replace(/,/g, '').substr(1));


var sw = parseFloat(sf[0].replace(/,/g, '').substr(1)) + parseFloat(sf[1].replace(/,/g, '').substr(1)) + parseFloat(sf[2].replace(/,/g, '').substr(1)) + parseFloat(sf[3].replace(/,/g, '').substr(1));


var cs = parseFloat(co[0].replace(/,/g, '').substr(1)) + parseFloat(co[1].replace(/,/g, '').substr(1)) + parseFloat(co[2].replace(/,/g, '').substr(1)) + parseFloat(co[3].replace(/,/g, '').substr(1));


//var cse = parseFloat(ms[0].substr(1)) + parseFloat(ms[1].substr(1)) + parseFloat(ms[2].substr(1)) + parseFloat(ms[3].substr(1));


var cap = parseFloat(cp[0].replace(/,/g, '').substr(1)) + parseFloat(cp[1].replace(/,/g, '').substr(1)) + parseFloat(cp[2].replace(/,/g, '').substr(1)) + parseFloat(cp[3].replace(/,/g, '').substr(1));


gs.print("Total Opex " + topex);


gs.print("Total Net " + neto);


gs.print("Total Am " + amz);


gs.print("Total Misc " + mi);


gs.print("Total Hardware " + hd);


gs.print("Total Software   " + sw);


gs.print("Total Consulting " + cs);


//gs.print("Total Net " + cse);


gs.print("Total CAPEX " + cap);




Here is the output



*** Script: Total Opex 6596350


*** Script: Total Net 6828580


*** Script: Total Am 0


*** Script: Total Misc 0


*** Script: Total Hardware 1110


*** Script: Total Software 11110


*** Script: Total Consulting 1133320


*** Script: Total CAPEX 1168770


Would that be alright if I help you out a little bit more? I want you to be ultra successful so I am going to share with you a different way to write that code so that it is less lines, a little easier on the eyes, and more straightforward.



var object_of_arrays = {


  opex: [],


  net: [],


  am: [],


  misc: [],


  hardware: [],


  software: [],


  consulting: [],


  capex: []


};




var sys = "87b4b72c3869310001a8fa55d9ddeb4b"


var bud = new GlideRecord('u_demand_budget');


bud.addQuery('u_demand.sys_id',sys);


bud.query();




while(bud.next()){


  object_of_arrays.opex.push(bud.u_total_operating_expenses.getReferenceDisplayValue() + '');


  object_of_arrays.net.push(bud.u_net_operating_expenses.getReferenceDisplayValue() + '');


  object_of_arrays.am.push(bud.u_amoritization.getReferenceDisplayValue() + '');


  object_of_arrays.misc.push(bud.u_misc_expense_retire_savings.getReferenceDisplayValue() + '');


  object_of_arrays.hardware.push(bud.u_hardware.getReferenceDisplayValue() + '');


  object_of_arrays.software.push(bud.u_software.getReferenceDisplayValue() + '');


  object_of_arrays.consulting.push(bud.u_consulting.getReferenceDisplayValue() + '');


  //object_of_arrays.tco.push(bud.u_misc_expense_retire_savings.getReferenceDisplayValue() + '');


  object_of_arrays.capex.push(bud.u_total_capital_expenses.getReferenceDisplayValue() + '');


}




for (var array in object_of_arrays) {


  var temp_counter = 0;


  for (var i = 0; i < array.length; i++) {


  temp_counter += parseFloat(array[i].replace(/,/g, '').replace(/\$/g,''));


  };


  gs.print('Total ' + array + ' ' + temp_counter);


};



I obviously cannot test it because I don't have your data or fields, but this should work. It is less lines of code. If ever you find yourself copying and pasting a lot (like all of your for loops or manually adding opex[1].replace(/,/g,'').substr(1)) a whole bunch of times, that is when loops come to the rescue! So I wrote this out for you and anyone else who can benefit from using loops instead of copying and pasting a bunch.