Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

Populate text in field 1 based on calculation in another field

Avatar

Former Community Member
Hello,



Currently, I have a calculated field that is the sum of 7 numeric fields. I then have a text field that I would like to populate based on the calculation.



For example, if the value in the calculated field (Total) is "27", textfield should read "Great Job". If Total is >27, the textfield should read "Keep Trying".



I have two questions... What script should I try? and Where should the script reside?



I have tried to add script in the 'change' section of the Total field, but since the user is not changing this field (it is calculated), the script is not being triggered. I have tried the script in the 'change' section of each of the 7 numeric fields, but am having trouble referencing the Total field value.



Any help would be appreciated.



Thanks



E
6 Replies

Avatar

Level 7
I would use the "calculate" action to sum the numeric fields and then test the result and set the text field accordingly.

Avatar

Former Community Member
Geo,



Thanks very much for the response. Let me explain a little further and maybe you can help provide the basic code to 'test the result'.



In the TOTAL field (Calculated - Read Only), I have placed "Field1 + Field2..." in the calculate action. My issue now is testing against that result and where to place that action. I place some script in the "change" action in each of the numeric fields (If TOTAL == "4", then textfield = "Good Job"). But it seems that this is always looking at the TOTAL one step prior to the final calculation.



Any ideas?



Thanks so much.

Avatar

Level 7
You may have to manually set the calculation order. I think the default order is the order in which the fields were created.

Avatar

Former Community Member
I'm sorry, but I'm still not getting it. Any sample script and where to put it would be great.



Sorry.

Avatar

Former Community Member
Eric,



I added 8 Numeric Fields named MonTotal, TueTotal, WedTotal, ThurTotal, FriTotal, SatTotal, SunTotal and WeekTotal.



I added a FormCalc script to the calculate event of the WeekTotal Numeric Field shown below:



form1.#subform[0].MonTotal + form1.#subform[0].TueTotal + form1.#subform[0].WedTotal + form1.#subform[0].ThuTotal

+ form1.#subform[0].FriTotal + form1.#subform[0].SatTotal + form1.#subform[0].SunTotal



Once I saw the calculation was working I inserted a new text field named "Comment" and a standard button (Labeled "Click on Completion" and added a FormCalc script to the click event of the standard button shown below:



if (form1.#subform[0].WeekTotal == 27) then

form1.#subform[0].Comment = "Great Job!!!"

elseif (form1.#subform[0].WeekTotal > 27) then

form1.#subform[0].Comment = "Keep Trying!!!"

endif



Hopefully this is helpful.

Have a great day!

Pam

Avatar

Former Community Member
Pam,



Thank you very much for your reply. I was hoping not to have an addition button that the user needed to click upon completion. My thought was that I could add script to the CHANGE section of each of the numeric fields that would somehow reference the Total field and the comment field.



For example, in the change section of the first numeric field, I had script that matched your above script for the button.



if (form1.#subform[0].WeekTotal == 27) then

form1.#subform[0].Comment = "Great Job!!!"

elseif (form1.#subform[0].WeekTotal > 27) then

form1.#subform[0].Comment = "Keep Trying!!!"

endif



However, it appeared that as I changed a number in the numeric field, the script was referencing the old total number, and not the newly changed total number. It always seemed to be behind one step.



Am I headed down a path that may not work?



Thanks,



Eric