Expand my Community achievements bar.

SOLVED

Trying to divide two numeric fields and have answer in third num field

Avatar

Level 3

Ok I am doing something wrong and can't figure it out.  I am using the below code inside the psf field of my form to divide two different numeric fields and for the life of me I can't figure out why it isn't working.  I'm assuming it is something simple.  I have uploaded the form.  Please take a look at the form and le me know where I am off.  It almost has to be something simple and something I'm just not seeing

<event activity="click" name="event__click">

<script contentType="application/x-javascript">

if (NumericField13.isNull || NumericField13.rawValue == 0)

{

NumericField28.rawValue = "0";

}elseif(NumericField27.isNull || NumericField27.rawValue == 0){

NumericField28.rawValue = "0";

}else{

NumericField28.rawValue = NumericField13.rawValue / NumericField27.rawValue;

}

</script>

</event>

I think it has something to do with how the division is being done.  I'm thinking that it is trying to divide by zero though at this point I have tried several differnt if/then statements and can't see where I am off in the equation so I tried the above as a click even and couldn't get that to work either.

Thank you in advance for your assistance.

Justin

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

First thing is that you have that script executing on the click event....so your code will not execute until a user clicks on the field. I suggest you put it

on the calculate event. That way whenever one of the other values named in the formula changes t

hen it will recalc this field.

Second there was a script error in your formula....I modified it to look like this (it just seems more logical this way to me)

if (NumericField13.isNull || NumericField13.rawValue == 0){
NumericField28.rawValue = "0";
}else {
if(NumericField27.isNull || NumericField27.rawValue == 0){
  NumericField28.rawValue = "0";
}else{
  NumericField28.rawValue = NumericField13.rawValue / NumericField27.rawValue;
}
}

Third your NumericField27 shodul have a default value of 1 or you should do acheck of its value so that it does not have a possibility of getting a 0 or your division will fail.

Lastly, you shodul rename your fields to have more descriptive names insstead of generic names like numericField XX. Many people liek to know the type of field by the name so instead of NumericField27 how about SQFT_NumFld? With this technique I can tell immeadiately which field you are referring to (Square Footage) as well as its type (Numeric Field). Just my 2 cents worth!

Paul

View solution in original post

1 Reply

Avatar

Correct answer by
Former Community Member

First thing is that you have that script executing on the click event....so your code will not execute until a user clicks on the field. I suggest you put it

on the calculate event. That way whenever one of the other values named in the formula changes t

hen it will recalc this field.

Second there was a script error in your formula....I modified it to look like this (it just seems more logical this way to me)

if (NumericField13.isNull || NumericField13.rawValue == 0){
NumericField28.rawValue = "0";
}else {
if(NumericField27.isNull || NumericField27.rawValue == 0){
  NumericField28.rawValue = "0";
}else{
  NumericField28.rawValue = NumericField13.rawValue / NumericField27.rawValue;
}
}

Third your NumericField27 shodul have a default value of 1 or you should do acheck of its value so that it does not have a possibility of getting a 0 or your division will fail.

Lastly, you shodul rename your fields to have more descriptive names insstead of generic names like numericField XX. Many people liek to know the type of field by the name so instead of NumericField27 how about SQFT_NumFld? With this technique I can tell immeadiately which field you are referring to (Square Footage) as well as its type (Numeric Field). Just my 2 cents worth!

Paul

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----