Question
· Sep 1, 2020

Router Business Rule, I want to check PID:7 DOB field and if date is within 3 days then I want to send HL7 to NewBornOperation

Hi 

I have a use case where for new born I want to send HL7 messages to different business operation. I created the business rule but in condition I am struggling to put syntax something similar to this

HL7.{PID:7} >= Today Date -3

any help would be appreciated. 

Discussion (5)3
Log in or sign up to continue

You can use DATEDIFF function to check the days between both date

set date1 = $zdate($horolog,3)
set date2 = $zdate($zdateh("2020-09-01",3),3)
write $SYSTEM.SQL.DATEDIFF("dd",date2,date1)     // 1

In your scenario it could be like this:

$SYSTEM.SQL.DATEDIFF("dd",$ZDATE($ZDATEH(HL7.{PID.7},3),3),$ZDATE($HOROLOG,3)) <= 3

This is a boolean condition, so your rule only have that condition

UPDATE

Rules only accept functions from functions class, so you should create a new class with that function:

Class MyFunctions Extends Ens.Rule.FunctionSet
{
ClassMethod DaysDobHL7(pDob As %String) As %Integer
{
    set date1 = $zdate($horolog,3)
    set date2 = $zdate($zdateh(pDob,3),3)
    
    quit $SYSTEM.SQL.DATEDIFF("dd",date2,date1)
}

then, your new function should be visible in rules function

Regards,
Francisco Lopez

Indeed.

The parameter 3 in $ZDATE and $ZDATEH is the format of the date YYYY-MM-DD ODBC format

$HOROLOG retrieves the system datetime, so $ZDATE($HOROLOG,3) gives the current sytem date

Usually, HL7.{PID.7} uses the format YYYY-MM-DD, so it converts the string into a datetime variable ($ZDATEH(dob,3))

Please, find out the info in $ZDATE and $ZDATEH documentation.

Best regards,
Francisco Lopez

P.S. Don't forget mark the answer as accepted wink

Hello Bukhtiar,

Not sure if you were just looking for the specific documentation that Francisco provided or something more general. If you want some resources on learning to work with InterSystems products (and specifically ObjectScript), I'd recommend reviewing the courses available on learning.intersystems.com. 

https://learning.intersystems.com/course/view.php?id=289

This course is "Learn Caché ObjectScript" and links to documentation, a tutorial, and an online course.

Here is a custom function that you may call from rules or transformations that will return the age in your choice of years, months, days.

Note that this is a complete export.

<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25" zv="Cache for Windows (x86-64) 2017.2.1 (Build 801U)" ts="2020-03-16 16:31:49">
<Class name="Custom.GetAge">
<Description>
Calculates and returns the age from the birthday passed in %Y%m%d format comparing to current date or passed in date.
ReturnUnits is "Y" (Default) for Years, "m" for Months, or "d" for Days
currentday in HL7 format, defaults to today if blank  </Description>
<ProcedureBlock>1</ProcedureBlock>
<Super>Ens.Rule.FunctionSet</Super>
<TimeChanged>65454,59370.735262</TimeChanged>
<TimeCreated>62585,49624.168815</TimeCreated>

<UDLText name="T">
<Content><![CDATA[
// ClassMethod GetAge(birthday As %String, ReturnUnits As %String, currentday As %String) As %String [ Final ]

]]></Content>
</UDLText>

<Method name="GetAge">
<Description>
returns the current age in years with birtday passed in %Y%m%d format</Description>
<Final>1</Final>
<ClassMethod>1</ClassMethod>
<FormalSpec>birthday:%String,ReturnUnits:%String="Y",currentday:%String=""</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[

    // convert date times into correct format
    set Tbirthformat = ##class(Ens.Util.Time).ConvertDateTime(birthday,"%Y%m%d","%q(3)" )
    if currentday '= ""
    {
       set Tcurrentformat = ##class(Ens.Util.Time).ConvertDateTime(currentday,"%Y%m%d","%q(3)")
    }
    else
    {   set Tcurrentformat = $h
    }
    
    set bmonth = $SYSTEM.SQL.DATEPART("mm",Tbirthformat)       
    set cmonth = $system.SQL.DATEPART("mm",Tcurrentformat)
    set bday = $system.SQL.DATEPART("dd",Tbirthformat)
    set cday = $system.SQL.DATEPART("dd",Tcurrentformat)
    set tretval = 0
    if (ReturnUnits = "Y") || (ReturnUnits = "y") || (ReturnUnits = "")
    {
      // calculate the difference in years by subtracting the birthday from the currentday
      set difftimeYear = $SYSTEM.SQL.DATEDIFF("yy",Tbirthformat,Tcurrentformat) // get the years

      if bmonth = cmonth   //check day since the month is the same
      {  
         if cday < bday
         {
           set difftimeYear = difftimeYear - 1
         }
      }
      else
      {  if cmonth < bmonth
         {
           set difftimeYear = difftimeYear - 1
         }
      }
      // return the age in years
      set tretval = difftimeYear
    }
    else
    { if (ReturnUnits = "M") || (ReturnUnits = "m")
      {
          set difftimeMonth = $SYSTEM.SQL.DATEDIFF("mm",Tbirthformat,Tcurrentformat)  // get the months
          if bmonth = cmonth
          { if cday < bday
            {  set difftimeMonth = difftimeMonth - 1
            }
          }
           // return the age in months
           set tretval = difftimeMonth
      }
      else
      {  if (ReturnUnits = "D") || (ReturnUnits = "d")
         { set difftimeDay = $SYSTEM.SQL.DATEDIFF("dd",Tbirthformat,Tcurrentformat)  // get the days
           // return the age in days
           set tretval = difftimeDay
         }
      }
    }
    quit tretval
]]></Implementation>
</Method>
</Class>
</Export>