as en example user could enter height data as cm or inch. regardless of what field they enter, I want to update the other automatically.
{! if height_inch <> "" then
height_cm = convert_inch_to_cm(height_inch)
else
height_cm=""
endif}
{! if height_cm <> "" then
height_inch= convert_cm_to_inch(height_cm)
else
height_inch=""
endif}
depending on which function run first, it is possible that both height_inch and height-cm are blank. How can I make sure the correct fun runs first?
Could you combine the two functions? Something like this:
{! if height_inch <> “” then
height_cm = convert_inch_to_cm(height_inch)
else if height_cm <> “” then
height_inch= convert_cm_to_inch(height_cm)
else
height_cm=””
height_inch=””
endif
endif}
This way you have a bit more control over how the function is being fired. Otherwise I might look at trying to move the values into a obs term and fire the functions when an obs term is updated.
Thank you for your response @bhoover. I tried watcher expression using obs term and global variable and it has never quite worked
The problem with combining both as you suggested the function only gets activated when height_inch changes. If the changes are made to height_cm, the above function is skipped over.
I would approach it like this:
In your code area put the following. (assuming they are document variables) The code should execute every time there is a change to the corresponding document variable.
{convert_inch_to_cm(DOCUMENT.HEIGHT_INCH)}
{convert_cm_to_inch(DOCUMENT.HEIGHT_CM)}
Then in your functions make sure they set the proper values in the other fields. You could replace either or both DOCUMENT.HEIGHT* with observations if that is what you are using.
{convert_inch_to_cm(OBSNOW("HEIGHT"))}
I just created a form with two edit fields, HIGHT_IN and HEIGHT_CM, both document variables. The following code works to immediately convert inches to CM and visa-versa as soon as you click off the field. If you would like a copy of a working version of this form please send me an email.
{!convert_inch_to_cm(DOCUMENT.HEIGHT_IN)}
{!convert_cm_to_inch(DOCUMENT.HEIGHT_CM)}
{!
fn convert_inch_to_cm(inches){
local cm
if val(inches) > 0 then
cm = val(inches) * 2.54
cm = round(cm, 2)
DOCUMENT.HEIGHT_CM = str(cm)
endif
}
}
{!
fn convert_cm_to_inch(cm){
local inches = 0
if val(cm) > 0 then
inches = val(cm) / 2.54
inches = round(inches, 2)
DOCUMENT.HEIGHT_IN = str(inches)
endif
}
}
awesome that works. Thank you very much.
Also in the my original functions, I removed the watched "!" and that fixed the issue too.