How Fetch a Value Using Velocity and assign it to an Obs Field

Am writing some form . I want to fetch a value using velocity ,and assign it to an OBS Field eg

<lookup complexExpression="
                       #set( $wt = $fn.latestObs('5089').valueNumeric ) 
                       #set( $ht = $fn.latestObs('5090').valueNumeric ) 
                       #set( $bmi = $wt / ($ht * $ht)) 
                      $bmi
                        "/>

and then assign that variable to an Obs Field

jq(function() {
           setValue('bsa.value', $bmi           
           });
        </script>.

Is there a velocity function i can use to asign a value directly to a given specific Obs Field??

Or rather is there a jqery function i can use to fetch the latest Obs of a given concept
cc @dkayiwa

1 Like

i used a hacky way to get it ,

i placed the lookup element in an html element

                    <p id="valt" >
                         <lookup complexExpression="
                             #set( $wt = $fn.latestObs('5089').valueNumeric ) 
                             #set( $ht = $fn.latestObs('5090').valueNumeric ) 
                             #set( $bmi = $wt / ($ht * $ht)) 
                            $!{bmi}
                          "/>
                   </p>

and then accesd the value of the html element using jquery ,and assigned it to an obs field

       <script type="text/javascript">
          jq(function() {
           $("#valt").hide();
           var bsa = parseFloat($("#valt").text());
           setValue('bsa.value',bsa);     
           });
         
        </script>
3 Likes

I am guessing u were looking for a hack to get BMI… I had to do something similar though didn’t use complexExpression and instead took a longer route( will try to assimilate your approach too though) So i seem to be having some trouble when i call the earliestObs vs latestObs…I get back the last entered Obs when i use earliestObs and I end up with the first Obs I entered when I call the latestObs as seen in this PR https://github.com/METS-Programme/openmrs-module-ugandaemr-iss/pull/13/files#diff-293bb631be5f600040e06969745ee7e8R450 Could there be something I am missing English-wise or my code is just not well constructed…Thanks for the advice. @dkayiwa @mozzy @mogoodrich @ssmusoke

not really BMI , i had to compute some other value ,BSA , but based on the latest obs (height and weight)

Huh :flushed: :flushed: that is abit strange coz this formula is for BMI.

The result it also returns for BMI is wrong because it assumes $ht is already in metres yet when being captured by a tool like capture vitals it is in centimetres… I have tried to manipulate the values within the compressExpression but without success because they give wrong results (Tried dividing $fn.latestObs(‘5090’).valueNumeric with 100 before it is used and also tried dividing each of the $ht in #set( $bmi = $wt / ($ht * $ht)) with 100.

@reagan , your right , but that was just a dummy formula for asking here ,

its not necesarily what i was implementing

1 Like

Alright…makes sense.

Have u had a chance to test out this? @mozzy

1 Like

not really ?? ill test that out .

1 Like