- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 05:45 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2015 10:16 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 07:13 AM
I'm not sure I understand the question. Each array element should be usable as an individual variable outside the loop provided you know which index you are looking for. Can you please provide some more information on what you are trying to do with these values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2015 10:01 AM
Carl,
You can do basically what you typed out: var something = fy[1].
There doesn't seem to be much reason to do that, however, because the whole point of an array is to make access to many values easy so that you don't have to make tons of variables.
I agree with Adam, we need more information about what you are trying to do so that we can point you in the right direction.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2015 11:19 AM
Here is what I am trying to do. I have the variable part figured out, now I am trying to add them together. Here is the Script:
var opex = [];
var sys = "857f2c0638ad310001a8fa55d9ddebef";
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[i]);
}
var topex = parseFloat(opex[0]) + parseFloat(opex[1]) + parseFloat(opex[2]);
gs.print(topex.toString());
Here is the result:
*** Script: $0.00
*** Script: $3.00
*** Script: $1.00
*** Script: $0.00
*** Script: NaN
How do I get topex to be a number?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2015 11:54 AM
You have to get rid of the dollar sign.
"parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed." parseFloat() - JavaScript | MDN
parseFloat(opex[0].substr(1));