Expand my Community achievements bar.

LC Javascript Help

Avatar

Level 2

Script Help Please:

(for form field bs) this.rawValue = bq.rawValue / br.rawValue


(Issue is field bq is input as 1.005 by the user and gets automatically rounded up to 1.01 as the display field  is only 2 decimal points as a requirement, but the input number above: 1.005(bq.rawValue) is what the script is using for the calculation but I want to use the rounded number (1.01)for the calculation (1.01)- how do I script this?

13 Replies

Avatar

Level 10

Hi,

The following in the calculate event should work if the Patterns in the fields are numeric (and not currency):

if (br.rawValue != null && br.rawValue != 0) {

     this.rawValue = bq.formattedValue / br.formattedValue;

}

Hope that helps,

Niall

Avatar

Level 2

Thanks so much Niall! I will test it out today, really appreciate the help.

Avatar

Level 2

Hi Niall, this works for the original script I posted but I am unable to get it to work for another similar field in the same form

and am hoping that you can spot my error as I’m sure it’s straightforward but I seem to be missing it.

Current Code: (same issue as previous, user enters a number with .006 and it rounds up to .01 but calculation is based on .006 and should be .001)

this.rawValue = bm.rawValue * bn.rawValue

Code I tried based on your code:

if (bn.rawValue != null && bn.rawValue != 0) {

this.rawValue = bm.formattedValue * bn.formattedValue;

}

Tried this code as well:

this.rawValue = bm.formattedValue * bn.formattedValue

Thanks, Kim

Avatar

Level 10

It might fail if one of the fields does not have a display pattern (eg formattedValue).

Also your second script is better as you don't need to test for null.

Avatar

Level 2

You’re correct, neither of the fields are a formattedValue as they are both user entered, but if I put in rawValue in the code it goes back to the same calculation error of calculating on the user input data rather than the rounded/display number.

Also, if you meant this, I checked the display pattern and all cells are set to: num{zzz,zzz,zz9.99}

So, field bj is user entered at 100.006 it rounds/displays as 100.01, field bk is user entered at 10.006 it rounds/displays as 10.01 – therefore: field bl is the calculation of bj * bk = bl and bl won’t display the calculation if it’s over 4 digits (displays 1000.00 but not 10,000.00 so it’s calculating, I just can’t seem to impact it to make it display on the form.

Not sure if this info helps or confuses, hopefully you can make sense.

Kim

Avatar

Level 2

Just to clarify:

(for field bl) This works, until field bj has a number over 999.00 in it then it fails?

this.rawValue = bj.formattedValue * bk.formattedValue

Field bj when entered as anything under 999.00 does the calculation properly and the calculation appears in bl properly BUT as soon as I put in a number over 1000.00 it fails, now this is with the formattedValue coding BUT doesn’t fail if I replace it with rawValue and change nothing else, makes no sense to me so I’m still poking around to see whatelse I can determine.

Previous msg:

You’re correct, neither of the fields are a formattedValue as they are both user entered, but if I put in rawValue in the code it goes back to the same calculation error of calculating on the user input data rather than the rounded/display number.

Also, if you meant this, I checked the display pattern and all cells are set to: num{zzz,zzz,zz9.99}

So, field bj is user entered at 100.006 it rounds/displays as 100.01, field bk is user entered at 10.006 it rounds/displays as 10.01 – therefore: field bl is the calculation of bj * bk = bl and bl won’t display the calculation if it’s over 4 digits (displays 1000.00 but not 10,000.00 so it’s calculating, I just can’t seem to impact it to make it display on the form.

Not sure if this info helps or confuses, hopefully you can make sense.

Kim

Avatar

Level 2

Addendum/Update:

After further testing I have discovered the following: the calculation fails when more than 3 digits are entered (999.00 vs 1000.00) the field displays the number (1000.00 for example) but fails the script calculation. So it appears to be connected to the formattedValue command that somewhere needs the parameters set to accept over 999.00. Is there a way I can define this formattedValue field or is there another approach.

I feel we’re close, thanks again Naill!

Avatar

Level 10

Okay,

If you use a Display pattern that does NOT have thousand separators: For example: num{zzzzzzzzzzz9.99}. This will resolve the limit on more that three digits.

Niall

Avatar

Level 2

Worked like a charm, thanks again Niall!

Avatar

Level 2

Just when I thought I may have worked through all the issues another one pops up…

At the bottom of this column all I want to do is a sum of the column but as is the theme of this form the addition/sum has to be of the formatted/rounded number and not the number entered by the user, so same issue as the original but it seems that (addition) + might need a different solution?.

(each of these fields have a calculation script applied to them:

this.rawValue = bq.formattedValue / br.formattedValue

Current coding: (this is a sum for field ‘ed’) This script does calculate in field ‘ed’ but the calculation is based on the entered (100.006) vs the rounded (100.01)

this.rawValue = bj.rawValue + bm.rawValue + bs.rawValue + by.rawValue + ce.rawValue + ck.rawValue + cq.rawValue + cw.rawValue + dc.rawValue + di.rawValue + d_o.rawValue + du.rawValue + ea.rawValue + ea1.rawValue + ea2.rawValue + ea3.rawValue

I also tried: (along with setting all the field patterns to num{zzzzzzzz9.99} )

this.rawValue = bj.formattedValue + bm.formattedValue + bs.formattedValue + by.formattedValue + ce.formattedValue + ck.formattedValue + cq.formattedValue + cw.formattedValue + dc.formattedValue + di.formattedValue + d_o.formattedValue + du.formattedValue + ea.formattedValue + ea1.formattedValue + ea2.formattedValue + ea3.formattedValue

Avatar

Level 2

Additional Info might make it clearer:

Fields: bs & by need to be added together (there are 16 fields to add together in total but for explanation purposes I’ll use 2)

The calculation is done in field ed which currently has the formula this.rawValue = bs.rawValue + by.rawValue applied to it and does the calculation of all 16 fields, except the calculation is based on the user entered number (100.006) but I want to base the calculation on the rounded number (100.01) so I applied the previous coding to it by using this.rawValue = bs.formattedValue + by.formattedValue and it fails after 2nd number is input (field ed shows the correct total for the 1st number but then zeros out when the 2nd number is input (0.00) whereas the last issue I had the field just went blank (not sure if that’s relevant but the more information you have the better!)

So the only difference in this calculation seems to be the fact that it’s addition and the previous was division/multiplication, just not sure why that would be relevant.

Hope that is a bit clearer.

Again, can’t thank you enough.

Avatar

Level 10

Hi,

I suspect that you are now using a reserved word as an  object name. For example "by" is probably reserved and therefore is probably causing the script to fail. Like d_0, change it to b_y or something else.

I am sure that this will work, but if it doesn't then we may need to set the data type to a number. Only try this once you have changed out the reserved word and it is still not working:

this.rawValue = Number(bs.formattedValue) + Number(b_y.formattedValue);

Good luck,

Niall

Avatar

Level 2

Hi Naill! Got it to work using the Number(bs.formattedValue) formula (I also went through and made sure all the field names were not script words (b_y).

Thanks again for all your help on this!

Kim