I am having trouble with this code. I have it set up to help us met the colonoscopy measure to run in a form that's in every office visit. Since we use OBS to document our colonoscopies and the CQR system only goes back 5 years I have had to become creative to meet this measure.
The code searches for last colonoscopy using the OBS term. If its between 5-10 yrs old it adds the SNOMED code using the date of the colonoscopy OBS. This code is working for me fine. However, I only want it to do it once. I have it set up to do it once per visit but once I start a new visit it adds another SNOMED code.
I have added the OBSNOW of "HXPREVCOLON" to the code. This works. But I looped in to search for OBSPREV so it wouldn't keep adding the SNOMED code. However, no matter what I do it doesn't read the OBSPREV("HXPREVCOLON") OR any variation of LAST_SIGNED_OBS_. (I have tried many different variations, this is just the last attempt before I asked for help):
I am clearly missing something. Any ideas?
{if document.checkLOOP == "" then if LASTOBSDATE("COLONOSCOPY")=="" then "" else if OBSPREV("HXPREVCOLON")<>"" THEN "" ELSE IF (patient_age()>=50 AND patient_age()<=75) AND DURATIONDAYS(LASTOBSDATE("COLONOSCOPY"),str(._TODAYSDATE)) >1825 AND DURATIONDAYS(LASTOBSDATE("COLONOSCOPY"),str(._TODAYSDATE)) <3650 then
local D1 = LAST_SIGNED_OBS_DATE("COLONOSCOPY") local T1= MEL_ADD_ORDER("S", "PQRI", "SNOMED-CT 73761001: Procedure, Performed: Colonoscopy", "", "", "", "", "", "", "", D1) t1="" document.checkLOOP = "done" + OBSNOW("HXPREVCOLON","done") else "" endif endif endif endif}
{! document.checkLOOP}
Sometimes obsterms can have 'issues' when initializing, so it is better to account for actionable status rather than 'do nothing' status. I've rewritten the code below to demonstrate what this would look like.
I have two suggestions that may help alleviate the initialization issues:
1) Convert this watcher into a function using the two obsterms referenced as 'triggers' - however ensure that you call the obsterm inside the function by assigning it to a variable and do not rely on values being passed via the parameter/argument. This is preferred due to obsterms being initialized at different times during MEL execution and causing the EMR to fire functions referencing them as arguments, which can and does produce unexpected results. By referencing them inside the function, they are properly initialized when called individually for assignment.
2) If the below and the suggestion above do not solve the issue, you may need to write a sub-function to loop through ORDERS_ALL, check the date, and determine if a new order is warranted - exit the loop when the date range is exceeded. Caution - ORDERS_ALL is a monster to parse, so use it carefully as it could adversely impact system performance.
I might also mention that your evaluation of HXPREVCOLON initially looks at OBSPREV and you assign it a value of 'done' at the end of the code, however when the form is re-opened, the previous value is still 'null', so it will meet the condition again, probably adding the order again. Perhaps evaluating 'lastobsvalue("HXPREVCOLON")' would be better suited for your needs.
Hope this helps.
{
if document.checkLOOP == "" then
if LASTOBSDATE("COLONOSCOPY") <> "" then
if LAST_SIGNED_OBS_VALUE("HXPREVCOLON") == "" then
if (patient_age() >= 50 AND patient_age() <= 75) then
local nDaysSinceLastColo = DURATIONDAYS(LASTOBSDATE("COLONOSCOPY"),str(._TODAYSDATE))
if (nDaysSinceLastColo > 1825 and nDaysSinceLastColo < 3650) then
local CkColo_D1 = LAST_SIGNED_OBS_DATE("COLONOSCOPY")
local CkColo_T1 = MEL_ADD_ORDER("S", "PQRI", "SNOMED-CT 73761001: Procedure, Performed: Colonoscopy", "", "", "", "", "", "", "", CkColo_D1)
CkColo_T1 = ""
OBSNOW("HXPREVCOLON","done")
document.checkLOOP = "done"
else ""
endif
else ""
endif
else ""
endif
else ""
endif
else ""
endif
}
{! document.checkLOOP}
Note: The 'forum' tends to remove text from code, so be certain to review it carefully.
Thank you Lee for your very thought-out answer. I must admit your answer was a little advanced for me. I think I understood what you were suggesting but I am not sure.
I tried changing the code to call function out like this:
{!If LASTOBSDATE("COLONOSCOPY") <>"" THEN DOCUMENT.COLOCH = "1" ELSE "" ENDIF}
{!If LASTOBSDATE("HXPREVCOLON") <>"" THEN DOCUMENT.COLOHX = "1" ELSE "" ENDIF}
{
if document.checkLOOP == "" then
if DOCUMENT.COLOCH <> "" then
if DOCUMENT.COLOHX == "" then
document.checkLOOP = "done"
else ""
endif
else ""
endif
else ""
endif
else ""
endif
else ""
endif
}
if (patient_age() >= 50 AND patient_age() <= 75) then
local nDaysSinceLastColo = DURATIONDAYS(LASTOBSDATE("COLONOSCOPY"),str(._TODAYSDATE))
if (nDaysSinceLastColo > 1825 and nDaysSinceLastColo < 3650) then
local CkColo_D1 = LAST_SIGNED_OBS_DATE("COLONOSCOPY")
local CkColo_T1 = MEL_ADD_ORDER("S", "PQRI", "SNOMED-CT 73761001: Procedure, Performed: Colonoscopy", "", "", "", "", "", "", "", CkColo_D1)
CkColo_T1 = ""
OBSNOW("HXPREVCOLON","done")
{! document.checkLOOP}
That didn't work so I tried matching the order like this:
{
if document.checkLOOP == "" then
if LASTOBSDATE("COLONOSCOPY") <> "" then
if match(ORDERS_ALL(), "SCT-73761001")==0 then
if (patient_age() >= 50 AND patient_age() <= 75) then
local nDaysSinceLastColo = DURATIONDAYS(LASTOBSDATE("COLONOSCOPY"),str(._TODAYSDATE))
if (nDaysSinceLastColo > 1825 and nDaysSinceLastColo < 3650) then
local CkColo_D1 = LAST_SIGNED_OBS_DATE("COLONOSCOPY")
local CkColo_T1 = MEL_ADD_ORDER("S", "PQRI", "SNOMED-CT 73761001: Procedure, Performed: Colonoscopy", "", "", "", "", "", "", "", CkColo_D1)
CkColo_T1 = ""
document.checkLOOP = "done"
else ""
endif
else ""
endif
else ""
endif
else ""
endif
else ""
endif
}
Neither worked for me. I might just end up having to use a button that calls the function. That way it avoids over doing the orders. I was just trying to make it easier. Thank you!
Sorry my first attempt to your answer I pasted in with the wrong code. It was like this:
{!If LASTOBSDATE("COLONOSCOPY") <>"" THEN DOCUMENT.COLOCH = "1" ELSE "" ENDIF}
{!If LASTOBSDATE("HXPREVCOLON") <>"" THEN DOCUMENT.COLOHX = "1" ELSE "" ENDIF}
{if document.checkLOOP == "" then
if DOCUMENT.COLOCH <> "" then
if DOCUMENT.COLOHX == "" then
if (patient_age() >= 50 AND patient_age() <= 75) then
local nDaysSinceLastColo = DURATIONDAYS(LASTOBSDATE("COLONOSCOPY"),str(._TODAYSDATE))
if (nDaysSinceLastColo > 1825 and nDaysSinceLastColo < 3650) then
local CkColo_D1 = LAST_SIGNED_OBS_DATE("COLONOSCOPY")
local CkColo_T1 = MEL_ADD_ORDER("S", "PQRI", "SNOMED-CT 73761001: Procedure, Performed: Colonoscopy", "", "", "", "", "", "", "", CkColo_D1)
CkColo_T1 = ""
OBSNOW("HXPREVCOLON","done")
document.checkLOOP = "done"
else ""
endif
else ""
endif
else ""
endif
else ""
endif
else ""
endif
}
{! document.checkLOOP}
Sorry for the delay in responding.
The code below uses a watcher and two functions to hopefully do what you want (If I understand your needs here). The watcher is triggered by changes in either obsterm.
The first function first checks if the functions needs to fire, then if the age is appropriate, then if an order is needed using the second function (the second function checks if the order exists and is less than 10 years old). If any are false, the function exists without any real burden on the system. I
If all are true, then it does what you indicated. Because the order was determined as not existing, it will only add the order one time.
Hopefully this will do what you want or at least give you ideas on what you can do to achieve that goal.
Hope this helps!
{
document.checkLOOP = fnCheckColoStatus(str(LASTOBSDATE("COLONOSCOPY"),LASTOBSDATE("HXPREVCOLON")))
}
{! document.checkLOOP}
{! fn fnCheckColoStatus(strTrigger)
{
local retStr = ""
local strBuf = ""
local strColoDate = ""
local strPrevDate = ""
local nDaysSinceColo
if document.checkLOOP == "done" then return "done" else "" endif
if (patient_age() 75) then return "done" else "" endif
if fnCheckOrderDate("SNOMED-CT 73761001: Procedure, Performed: Colonoscopy") == "exists" then return "done" else "" endif
strColoDate = LASTOBSDATE("COLONOSCOPY")
strPrevDate = LASTOBSDATE("HXPREVCOLON")
nDaysSinceColo = DURATIONDAYS(LASTOBSDATE("COLONOSCOPY"),str(._TODAYSDATE))
if strColoDate "" then
if strPrevDate == "" then
if (nDaysSinceColo > 1825 and nDaysSinceColo < 3650) then
strBuf = MEL_ADD_ORDER("S", "PQRI", "SNOMED-CT 73761001: Procedure, Performed: Colonoscopy", "", "", "", "", "", "", "", strColoDate )
OBSNOW("HXPREVCOLON","done")
document.checkLOOP = "done"
else ""
endif
else ""
endif
else ""
endif
return ""
}
}
{! fn fnCheckOrderDate(strOrdDesc)
{
local retStr = ""
local strTemp = ""
local strBuf = ""
local i
local strList = ""
local strOrdDate = ""
strList = ORDERS_ALL("delimited")
strTemp = getfield(strList,"|","")
for i = 1, i <= size(strTemp), i = i + 1 do
strTemp[i] = getfield(strTemp[i],"^","")
if DURATIONDAYS(strTemp[i][3], str(._TODAYSDATE)) <= 3650 then
if strOrdDesc == strTemp[i][1] then
retStr = "exists"
else ""
endif
else
break
endif
endfor
return retStr
}
}