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
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).valueCoded.name"/> or <lookup complexExpression="$fn.latestObs(90315,$encounter.encounterDatetime).valueCoded.name"/>
The output is $fn.latestObs(90315,$visit.startDatetime).valueCoded.name which shows that the velocity expression is not processed.
Seems like I have resolved the problem instead of <lookup complexExpression="$fn.latestObs(90315,$visit.startDatetime).valueCoded.name"/> 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').valueCoded.name"/> works like a charm!!
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"/>