Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

basic if statement in formcalc..

Avatar

Former Community Member

I'm trying to create basic if statement for one of the fields. I'd like to know your opinion what is wrong with it

//code

form1.#subform[0].Body.TotalTaken[0]::calculate -  (FormCalc, client)

var sum = Sum(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)

// if I leave only Sum(Jan,...) in this field it works fine, shows the sum...

if (this.rawValue > 0 && TotalAvailable.rawValue <= sum)

  this.rawValue = sum

else

    this.rawValue = 1

endif

//endofcode

Im trying to do basic validation, if sum is between 0 and Total Available number then its ok, display the sum, other wise dosplay some popup, at the moment I want only "1" to be shown since I cant pass even that basic scripts.

I get an error "near token &". It doesnt matter if I switch "this" with field name, "TotalAvailable". All fields are numeric of same type.

Please advice.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

Sorry, not thinking straight...

There is also a "then" missing from the if statement:

var sum = Sum(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)

if ($ > 0 and TotalAvailable <= sum) then

$ = sum

else

$ = 1

endif

Good luck,
Niall

View solution in original post

4 Replies

Avatar

Level 10

Hi,

In FormCalc you don't need .rawValue (afaik) - some people use it but I don't. Also "this" is a javascript syntax, replace with "$" in FormCalc.

I would use a single "&" or the text "and" in FormCalc.

Lastly I am not sure what you are trying to achieve summing months. The Sum function is for numbers, whereas these are strings.

Niall

Avatar

Former Community Member

Sum() works fine, Jan, Feb etc are names of NumericFields.

I have holidays chart where user puts overall number of days available for him as free from work in field named "TotalAvailable"

in fields named after each month, Jan, Feb etc.. user puts how many days he will use for holidays in given month

In "TotalTaken" field I add all the days user took in any of given months.

With that if statement I want to check if Sum(..) is not bigger than TotalAvailable number, if is bigger then error (or just "1" for now as I have to much troubles with simpler tasks)

Ill try to use your tips and see what comes of it, thank you. Still any advice will be helpfull.

Best regards

Avatar

Correct answer by
Level 10

Hi,

Sorry, not thinking straight...

There is also a "then" missing from the if statement:

var sum = Sum(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)

if ($ > 0 and TotalAvailable <= sum) then

$ = sum

else

$ = 1

endif

Good luck,
Niall