Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

Trying to do something very simple using FormCalc

Avatar

Former Community Member
I have two address fields. If the 2nd one is empty, I want to set it to the value of the first one. I've added the below to the calculate event for the 2nd field. When I enter something in the 1st field and the 2nd field is empty, it puts a zero in the 2nd field. It happens when the Type on the Value tab is set to User Entered - Optional, Calculated - User Can Override, or Read Only.



FormCalc:



if (Len(InsuredMailingAddrAdvPymt)==0) then InsuredMailingAddrAdvPymt = InsuredMailingAddr endif



The 1st field is InsuredMailingAddr and the 2nd is InsuredMailingAddrAdvPymt



I'm amazed at how difficult doing something this simple is using Designer.



Chris Bacon
4 Replies

Avatar

Former Community Member
There is a logical issue with what you are trying to do.

As the form opened, both fields are empty. As the user going down the form to fill out the information, he/she has to fill out the first field before even get to the second field (normal user would do that :) ).

As soon as the users tab out of the first field, the 2nd field is empty still since he/she does not get a chance to fill it out.

Now, the script that you put on the "calculate" event will fire every time you exit a field. In this case, as soon as you exit the first field, the calculate event is fire. It will find field 2 is empty and assign whatever value in field 1 to field 2.



So I suggest to check for the value of field 2 to see whether or not it is null on the "exit" event of field 1.



You can try something similar to this on the "exit" event of field 1.



if (InsuredMailingAddrAdvPymt.rawValue == null) then

InsuredMailingAddrAdvPymt.rawValue = InsuredMailingAddr.rawValue

endif

Avatar

Former Community Member
Jimmy, thanks for your response.



"Now, the script that you put on the "calculate" event will fire every time you exit a field. In this case, as soon as you exit the first field, the calculate event is fire. It will find field 2 is empty and assign whatever value in field 1 to field 2."



The above is exactly what I wanted to happen, but what does happen is that field 2 is set to '0' (a zero).



Is there any difference between using Len(field1)==0 and field1.rawvalue==null?



What is the difference between field2 = field1 and field2.rawValue = field1.rawValue?

Avatar

Former Community Member
len(string)



the function len() only take the string as parameter and return the length of that string. It does not take a form field as a parameter. So in len(field1), field1 is not a valid parameter. However, you can check the length of the value enter in field 1 by using len(field1.rawValue)



The statement (field1.rawValue == null) is the same as asking if field 1 is empty.



field1 = field2 ==> this is invalid statement.

field2.rawValue = field1.rawValue ==> What this means is take the value in field 1 and assign it to field 2.

Avatar

Former Community Member
OK, thanks, I thought that might be the case. It's curious, though, because I've successfully used numeric fields in FormCalc like:



numericField1 + numericField2



and I thought that text fields would behave the same. Thanks for your help. The help examples don't point show this type of thing.



Chris