Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Trying to avoid Divide by Zero error, please review script

Avatar

Former Community Member
Hi,



I have the following FormCalc script as a Calculate event. Can anyone please tell me what's wrong with it. I need to avoid a divide by zero error if someone enters 0 in either of the two referenced fields.



if (HasValue(EstimatedTotalEligibleCosts <= 0) or (EstimatedGMFGrant <= 0))

then this.rawValue =""

else EstimatedGMFGrant / EstimatedTotalEligibleCosts * 100

endif



Thank you.
2 Replies

Avatar

Level 7
Well it looks like your logical statement will always have a "true" value because the "HasValue()" funciton will always be presented with either "true" or "false" which are both values. You probably should be testing the values and the existence of the value separately. It also appears that you should be only using the "and" logical operator and not the "or" since you need all the values to have a value and to be less than or equal to zero. I would have changed the comparison to greater than zero and exchanged the operations for reading clarity.



if ( (EstimatedTotalEligibleCosts > 0) and (EstimatedGMFGrant > 0) and (HasValue(EstimatedTotalEligibleCosts) and HasValue(EstimatedGMFGrant) ) then

EstimatedGMFGrant / EstimatedTotalEligibleCosts * 100

else

this.rawValue =""

endif

Avatar

Former Community Member
Thank you Geo. I am using your edited script and getting an error at the 'then' statment. Should this be a calculate object or a validate object?