HTML Form Entry - Velocity Functions - Passing the current encounter date or visit date

I am currently working on which allows the following velocity functions

  • latestObs() as latestObs(Date)
  • latestEncounter() as latestEncounter(Date)
  • latestEncounter(EncounterType) as latestEncounter(EncounterType, Date)
  • currentProgramWorkflowStatus(programWorkflowId) as currentWorkflowStatus(programWorkflowId, Date)

to be limited to a date. I have gotten the functionality working, however now the major use case that I needed the feature to be used is in an HTML form where by the date I am using is the encounter date for the form.

I have looked through the documentation, while there is <lookup expression="visit.startDatetime"/> I cannot use this in a complex expression like <lookup complexExpression="$fn.latestObs(90315).valueCoded.Id"/>

I am wondering can I create a velocity function to provide the current encounter date $fn.encounterDate() so that I can use that? for a new encounter it will take the current visit date while for an existing encounter it will take the saved encounter date

@mogoodrich @mseaton @mksd What are your thoughts on this?

Thanks @ball for the link at Re: [OpenMRS Talk] HTML Form Entry - Velocity Functions - Passing the current encounter date or visit date

However I have found that I cannot pass existing variables to a Velocity Expression, any suggestions on how I can do something like <lookup complexExpression="$fn.latestObs(90315,$visit.startDatetime)"/> or <lookup complexExpression="$fn.latestObs(90315,$encounter.encounterDatetime)"/>

The output is $fn.latestObs(90315,$visit.startDatetime) which shows that the velocity expression is not processed.

Any ideas on what I may be missing here?

Seems like I have resolved the problem instead of <lookup complexExpression="$fn.latestObs(90315,$visit.startDatetime)"/> there is a need to add single quotes around the velocity variable so that it can be evaluated hence <lookup complexExpression="$fn.latestObs(90315,'$visit.startDatetime')"/> works like a charm!!


Looks like this only works for printing the value, but not passing it as a variable to the function … So we are back to square 1

1 Like

Quoting the values will turn it into a string. Which is not what you want. Your syntax is correct without quotes. It is just that both $encounter and $visit are null.

@dkayiwa As per UgandaEMR approaches, every form only works when there is a past visit, so for new entry the visit date is used, while for edits, the encounter is used

Can you try this code

<lookup complexExpression="#if ($encounter) #set ($encounterDate = $encounter.encounterDatetime) #else #set ($encounterDate = $visit.startDatetime) #end"/>
Encounter Date and Time Variable <lookup complexExpression="$encounterDate"/>
                            <lookup complexExpression="$fn.latestObs(90315,$encounterDate).valueCoded.Id"/>

The encounterDate is null.

@dkayiwa This is what my page section looks like which shows that I have a value for the encounterDate variable

Let me try that out.

@dkayiwa Can you share with me your form so that I can also test with that

Add a function Obs latestObs(Integer conceptId, Date date) to VelocityFunctions and then try again.

@dkayiwa You are a genius, and a Godsend!! A week of juice and fruits on me when we next meet

I am actually updating to include similar methods for each of the new functions that I have added