Chris,
a watcher expression will fire when the form first opens (in most circumstances) and also whenever the variable or Obs Term you are "watching" changes (e.g. if you interact with a control on the form that is bound to that variable or Obs Term).
As Sandy Jo alludes, you have to be careful with watcher expressions, because they can fire unnecessarily if you don't keep track of the state of your form. For your purposes, it sounds like you want to define your watcher expressions with a conditional statement, especially if you only want it to execute one time. The quick fix is to incorporate a conditional statement into your watcher expression like this:
{!if OBSNOW(“Pharm Name”) == "" then OBSNOW(“Pharm Name”, "Pyxis")}
{!if {!OBSNOW(“Rx_Type”) == "" then OBSNOW(“Rx_Type”, "W")}
The above statements will likely accomplish your objective, however you might consider a standard solution to accommodate what other "event driven" languages do a much better job of than MEL. A common thing folks like to do is initialize variable values/OBS terms on a form the first time the form loads, and retain any changes to those variable values/OBS terms (if made) after the document is put on hold. While this might be overkill for your particular case, I use a hybrid strategy similar to Sandy Jo's which I've outlined below:
First, you need to create a variable that is tied/bound to a control on the form. This allows you to retain the form's state, even if it is put on hold. Since you don't want this control to be seen on the form, I recommend setting up a visibility region that will never display, and then include the control in that region. I typically name this variable IS_INITIALIZED.
Then I include a watcher expression at the top of the function view window as follows:
{!if DOCUMENT.IS_INITIALIZED == "" then
<insert code to initialize variables and OBS terms>
DOCUMENT.IS_INITIALIZED == "Yes"
endif
}
You can then insert whatever code you want to initialize variables, initialize OBS terms, or whatever code you want to run when the form first opens. You can use a similar strategy to keep track of other "form states" you want to track. For example, you could use other variables in the hidden visibility region to keep track of how many times a form has been opened, type of user that has opened the form, name of user that has opened the form, etc. and fire watcher expressions based on a particular condition.
The above strategy is what I consider best practice for keeping track of the form's state or the status of certain events.
I hope this provides more insight into the use of watcher expressions.
Greg
Posted : September 11, 2018 9:45 am