trakcare user defined functions

39
System Management TrakCare User Defined Functions Printed 16/07/2007 10:28 AM Changes 16/08/200 2 Initial release 10/09/200 2 Added UDF Programming & Samples 28/10/200 2 Added OnLabEpisode event 12/11/200 2 Restructured and added more UDF Programming 12/12/200 2 Identifying Worklfows & HasPrinted Method 23/06/200 3 Added ComputerName & GetPrinter methods 15/06/200 Added Implementing UDFs The information disclosed herein are confidential and proprietary to InterSystems. This information may not be used or disclosed to any third party for any purpose other than that specifically authorised in writing by InterSystems.

Upload: mauricioaguirre

Post on 21-Jul-2016

265 views

Category:

Documents


24 download

DESCRIPTION

TRAK

TRANSCRIPT

Page 1: TrakCare User Defined Functions

System Management

TrakCare User Defined Functions

Printed 16/07/2007 10:28 AM

Changes 16/08/2002 Initial release10/09/2002 Added UDF Programming & Samples

28/10/2002 Added OnLabEpisode event12/11/2002 Restructured and added more UDF Programming

12/12/2002 Identifying Worklfows & HasPrinted Method23/06/2003 Added ComputerName & GetPrinter methods

15/06/2003 Added Implementing UDFs11/08/2003 Added OnEpisodeDeposit to Event table

14/08/2003 Added OnAdmNoGen to Event table27/10/2006 Added msg.AddItem object description

The information disclosed herein are confidential and proprietary to InterSystems. This information may not be used or disclosed to any third party for any purpose other than that specifically authorised in writing by InterSystems.

Page 2: TrakCare User Defined Functions

TrakCare User Defined Functions

Table of Contents

Summary.......................................................................................................................................... 4

Pre-requisites.................................................................................................................................4

References.....................................................................................................................................4

UDF Definition................................................................................................................................5

Class Name...............................................................................................................................5

Event......................................................................................................................................... 6

CachéScript...............................................................................................................................7

Exporting & Importing User Defined Functions..........................................................................7

Exporting UDF Definitions from Cache explorer........................................................................7

Exporting UDF Routines from Cache Explorer..........................................................................8

Importing UDF Definitions from Cache Explorer........................................................................8

Importing UDF Routines from Cache Explorer..........................................................................9

Exporting UDF routine from TrakCare.......................................................................................9

Importing UDF Routines from TrakCare Web............................................................................9

Implementing UDFs.....................................................................................................................10

UDF Programming & Samples....................................................................................................10

OnSQLTrigger.........................................................................................................................10

OnBeforeSave..............................................................................................................................11

How to popup an Error message in OnBeforeSave?..............................................................11

How to popup a Warning message in OnBeforeSave?...........................................................12

msg.AddItem Object....................................................................................................................13

ClassMethod...........................................................................................................................13

OnNew..........................................................................................................................................14

How to default fields in the OnNew event?.............................................................................14

Identifying Components & Workflows.......................................................................................15

How to identify a specific component?....................................................................................15

How to identify a specific workflow?........................................................................................15

TrakCare Technical Documentation 2 of 25

Page 3: TrakCare User Defined Functions

TrakCare User Defined Functions

How to identify a specific workflow item?................................................................................15

How to find out workflow ids?..................................................................................................16

Using Class Objects....................................................................................................................16

How to investigate and update a record using class objects?.................................................16

How to read and update a multi-line field using class objects?...............................................17

How to use Designative Reference properties using class objects?.......................................18

%session & %request Objects...................................................................................................19

How to use the %session object?...........................................................................................19

How to use the %request object?............................................................................................19

Printing, Reporting and Emailing...............................................................................................20

How to print a report?..............................................................................................................20

How to setup parameters for a report?....................................................................................21

How to check if a specific report has been printed before?.....................................................21

How to print a report if a Print checkbox is checked?.............................................................22

How to save a Crystal Report as a file?..................................................................................22

How to email a message?.......................................................................................................23

How to Print/Email a Word Template?....................................................................................24

Methods........................................................................................................................................24

How to determine the Computer Name from IP Address?......................................................24

How to determine what the Destination Printer will be from Print Manager?..........................24

Language Reference....................................................................................................................25

GetIdFromCodeOrDescription.................................................................................................25

SetObjectId..............................................................................................................................25

TrakCare Technical Documentation 3 of 25

Page 4: TrakCare User Defined Functions

TrakCare User Defined Functions

SummaryUser Defined Functions are Cache script routines that perform site-specific logic.

The anatomy of a UDF is made up of two parts:

1. The user defined function definition.

2. The user defined function CachéScript routine(s).

Importing and exporting procedures require exporting both parts out to disk, and then restoring from disk.

Pre-requisitesNone.

ReferencesNone.

TrakCare Technical Documentation 4 of 25

Page 5: TrakCare User Defined Functions

TrakCare User Defined Functions

UDF DefinitionUDFs are defined in the Side Menu->ToolBox->UDFs. Click New to create a UDF.A UDF definition is made up of:

Class Name

Event

Cache UDF Program

Class NameThe web pages that deliver content are constructed from underlying Cache object classes defined by TrakCare. When the new, update or delete link on these web pages are clicked, the appropriate method of these classes is invoked.

A UDF event branches out from this action, and can take a number of paths, based on whether one is deleting, updating or creating a new record.

In order to determine what the underlying Class name of a given web page is, do the following:

Call up the specific web page/component to which you want to add a UDF

Enter the Layout Editor

Choose the menu option Tools > SQL Explorer to display the properties of the underlying class.

The title bar of the window being displayed is made up of the letters ‘User.classname’. This is referred to as the full class name. For example, in the case of ‘User.PAAdm’, the class name you would require for your UDF is PAAdm.

This applies to all the events except the OnSQLTrigger event. You must use the full class name for all OnSQLTrigger events, e.g. User.PAAdm. This also applies to questionnaire components where the full class name must be used.

Also for websys classes, the classname you would require for your UDF is the same as the others, e.g. websys.ReportXRef – the classname would be ReportXRef.

TrakCare Technical Documentation 5 of 25

Page 6: TrakCare User Defined Functions

TrakCare User Defined Functions

EventThe Event allows you to allocate different logic against new record, existing record or deleting record updates.

Events

Event Code Description Parameters

OnBeforeSave This event allows you to allocate specific logic that will be executed before the web component is updated.

Id

OnAfterSave This event allows you to allocate specific logic that will be executed after the web component is updated.

id

OnNew This event allows you to allocate specific logic that will be executed before a NEW web component is displayed. Use this method to populate default values.

None

OnDelete This event allows you to allocate specific logic that will be executed before the Delete link on a web component is clicked.

id

OnSQLTrigger This event is triggered whenever a change occurs to the SQL table that is represented by the web class, i.e. for every INSERT, UPDATE or DELETE. Generally the SQL Trigger will be executed before the other events, i.e. the SQL Trigger event for a deletion will be triggered before the OnDelete event.

OnSoundex Allows the user to write their own site specific version of Soundex. The OnSoundex event can only be defined against the PAPerson class.

OnLabEpisode This event is triggered on the Insert of a Lab Order Item

OnEpisodeDeposit Allows a default amount to be defined when an episode specific deposit is being created. The OnEpisodeDeposit event can only be defined against the ARReceipts class.

EpisodeID

OnAdmNoGen Allows a site specific admission number to be generated, function should return the number. The OnAdmNoGen event can only be defined against the PAAdm class.

OnAutoPack This automatically packs pharmacy items that are sitting in the pharmacy queue and the logic is based on vb. You don’t need any input id for it.

OnBarCode This returns the oldest batch and qty with the Barcode search

barcode,Loc

OnCustomDraw Used by CustomItem on component:PAPerson.Banner patientid

OnPatientDeceased This returns deceased patient ‘s admission data Episode Id

TrakCare Technical Documentation 6 of 25

Page 7: TrakCare User Defined Functions

TrakCare User Defined Functions

CachéScriptA CachéScript routine is the Caché UDF program that will contain the logic that will be executed when a particular event is invoked. All user defined Caché programs must start with the uppercase letter ‘U’, in order to avoid

them being overwritten by any patches TrakHealth distributes.

All UDF's should follow the same naming convention. We also suggest that the logic for ALL events for a class are placed in the same UDF Cache Program, i.e. [event]^U[site][classname] for example:

$$OnAfterSave^USITEPAAdm

^USITEPAAdm is the name of the CacheScript routine (i.e. USITEPAAdm.MAC) and $$OnAfterSave is the tag\label inside the routine.

All CacheScript names are case-sensitive. Also you will find that you are not able to type up-arrow character (^) in any Textbox in TrakCare. You will need to type the whole string in for example, notepad and then copy and paste.

If the UDF expects to receive a parameter then you need to pass it to the function, e.g. $$OnAfterSave^USITEPAAdm(id) otherwise leave it blank, e.g. $$OnNew^USITEPAAdm().

Exporting & Importing User Defined Functions.Exporting UDF Definitions from Cache explorer

The UDF definitions that are created under the Toolbox are stored in the globals websys.UserDefinedFunctionsI and websys.UserDefinedFunctionsD. To export UDF definitions from a TrakCare system follow the below steps:1. Start the Cache Explorer window

2. Via Namespace on the left hand side, select the WEBSOURCE namespace under that, select Globals. The list of globals in this namespace should display in the right-hand side pane.

3. In the text box above the right-hand pane, type in the following, in the correct case:

4. websys.UserDefinedFunctions* Then hit F5 to refresh the display.

5. Two globals should now display:

6. Select the menu option File|Export…Ctrl+E and click on Save to Disk…

7. Enter filename and save to disk

TrakCare Technical Documentation 7 of 25

Page 8: TrakCare User Defined Functions

TrakCare User Defined Functions

Exporting UDF Routines from Cache ExplorerThe UDF Cache Routines that are created are stored in the WEBSOURCE namespace. To export UDF programs from TrakCare system follow the below steps:1. In the text box above the right-hand pane, type in a new selection criteria as follows:

2. U*.MAC

3. Via Namespace on the left hand side, select the WEBSOURCE namespace, under that, select Routines.

4. The right hand side will now start to show all the *.MAC routines that begin with ‘U’ that exist in this namespace.

5. The list of routines in this namespace should display in the right-hand side pane. It is important that you view the status bar of this window (at the bottom). This will say Fetching… whilst the list is still being populated.

6. Select the menu option Edit > Select All to select all routines

7. Select the menu option File|Export…Ctrl+E and Save to Disk

Importing UDF Definitions from Cache ExplorerTo Import User Defined Functions definitions into a TrakCare system:1. Start the Cache Explorer window

2. Via Namespace on the left hand side, select the WEBSOURCE namespace, under that, select Globals. The list of globals in this namespace should display in the right-hand side pane.

3. In the text box above the right-hand pane, type in the following, in the correct case:

websys.UserDefinedFunctions* Then hit F5 to refresh the display.

Two globals should now display:

websys.UserDefinedFunctionsD and websys.UserDefinedFunctionsI

4. Select the menu option Edit > Select All to select these two globals

TrakCare Technical Documentation 8 of 25

Page 9: TrakCare User Defined Functions

TrakCare User Defined Functions

5. Select the menu option File|Delete and confirm deletion of these files

6. Right-mouse click in the right-hand pane and choose Import from Disk…

7. Specify the location where you placed the exported UDF

8. Click Open and then Import all. Click Close when done.

Importing UDF Routines from Cache Explorer1. Via Namespace on the left hand side, select the WEBSOURCE namespace

2. On the right hand side, right-mouse click Routines, and select Import from Disk…

3. Specify the location where you placed the exported UDF routines

4. Select the file name for this group of routines

5. Click Open and then Import all. Click Close when done.

Exporting UDF routine from TrakCare1. Logon to TrakCare web Application

2. Go to side menu Toolbox->Xml Export and below screen pops up.

3. Make sure to select a suitable site code and go to File and choose Add UDF option

4. After choosing an UDF go to File Save option and then save it to a file. Example \Gecko\temp\testexport.xml

Importing UDF Routines from TrakCare Web1. Logon to TrakCare web Application

2. Go to side menu Toolbox->Xml Import and then go to File Open and select the file which was saved through XML Export refer above mentioned steps. Example \Gecko\temp\testexport.xml

3. Make sure to choose a site code and press OK.

TrakCare Technical Documentation 9 of 25

Page 10: TrakCare User Defined Functions

TrakCare User Defined Functions

Implementing UDFsTrakHealth do not distribute UDFs. The UDFs are maintained by the remote site/Partner.

As UDFs are Cache routines ALWAYS make developments in the MAC. Never make developments in the .INT of the UDF because if someone compiles the MAC then the changes in the INT will be lost.

Add comments to the top of the UDF detailing the changes made, log number & your initials. This will help tremendously in providing information on what UDFs are the correct versions.

Remember to add comments throughout the code that you write to make it easier for other people to maintain the UDF in the future. By the nature of site specific functionality, changes will be required on an ongoing basis therefore it is important to comment your code.

As UDFs are owned by the site it is very important that the ‘Development’ environment is backed up on a regular basis.

Use the XML Import/Export utility in the Layout Editor to move UDFs from environment to environment.

The structure of a UDF is very important to how easy it can be maintained. Do not put all the code in the top level of the Cache Routine. Create other Cache routines which hold the specific event logic, e.g. the UDF routine USITEPAAdm calls another routine called USITEPAAdmReports whenever a report is required to be printed. Also create a routine which stores any generic functions that you create.

UDF Programming & SamplesOnSQLTriggerHow to determine the Type of an OnSQLTrigger Event?

Use the object %oper to determine which SQL event triggered OnSQLTrigger event.

UPAPerson ; User defined functions for class PAPerson;;called for every Insert/Update/Delete

OnSQLTrigger();%oper will return either INSERT, UPDATE or DELETEset ^zTMP("PAPerson","Type",$p($h,",",2))=%oper;quit 1

TrakCare Technical Documentation 10 of 25

Page 11: TrakCare User Defined Functions

TrakCare User Defined Functions

How to determine the ID of the record in an OnSQLTrigger Event?Use the object %d(1) to determine the ID of the record in an OnSQLTrigger event.

UPAPerson ; User defined functions for class PAPerson;;called for every Insert/Update/Delete

OnSQLTrigger();returns the id of the current recordset ^zTMP("PAPerson","ID",$p($h,",",2))=%d(1);quit 1

OnBeforeSaveHow to popup an Error message in OnBeforeSave?

To stop the update and display a popup message to the screen, quit the OnBeforeSave event with a ‘0’. The update of the record will only occur if you quit with ‘1’. In other words, if you quit with '0' then the update will not occur. The user will be forced back to re-enter valid data. If you quit with '1' then the record will be updated.

In the below example an error message displays when the registration number is not 6 characters in length and when it’s not numeric.

UPAPerson ; User defined functions for class PAPerson;

; Called before saving in websysSaveOnBeforeSave(id)

new regno,validset valid=1; get registration number from the pageset regno=%request.Get("RegistrationNumber")set ^zTMP($zn,"OnBeforeSave","regno")=regnoif regno'="" { ;if registration number is not 6 digits if $length(regno)'=6 set valid=0 ;if registration number is not numeric if $translate(regno,"1234567890")'="" set valid=0

}; add error message ready for displayif 'valid do:msg msg.AddItem("E","Invalid Registration Number",regno)set ^zTMP($zn,"OnBeforeSave","valid")=validquit valid

The error message will appear similar to below:

Clicking <OK> will return you to the previous Edit screen.

TrakCare Technical Documentation 11 of 25

Page 12: TrakCare User Defined Functions

TrakCare User Defined Functions

How to popup a Warning message in OnBeforeSave?See below for the basic structure to use when applying a Warning message to a UDF:

OnBeforeSave(id) ;n compref,compid,overrides compref=%request.Get("TEVENT"),compid=$e($p(compref,"i",1),2,999)s override=%request.Get("TOVERRIDE")if override'=1 {

; This line will set the request object 'TOVERRIDE' to '1' ; if user presses [OK] or "", if user presses [CANCEL]

Do msg.AddItem("W","Value","Error message " ,compid,"")}

if override'=1 q 0;; Continue with the rest of the OnBeforeSave logic.; Q 1

See below for an example:

UPAPerson ; User defined functions for class PAPerson;; Called before saving in websysSave

OnBeforeSave(id); show warning message if existing patient with same surname and firstname; if new registration and if user clicks OKif id="",%request.Get("TOVERRIDE")'=1 { new surname,firstname,first set (surname,firstname,first)="" ; set surname=%request.Get("PAPERName") set firstname=%request.Get("PAPERName2") ;

; check surname and first name set

first=$o(^PAPERi("SGD",##Class(%Library.Collation).AlphaUp(surname),##Class(%Library.Collation).AlphaUp(firstname)_" ",0))

; ; if name already exists display warning message

if first'="" do msg.AddItem("W",surname_", "_firstname,"An existing patient already has this name, continue?",compid,"")

};quit 1

The warning message will appear similar to below:

Click <OK> to continue or click <Cancel> to return to the previous screen.

TrakCare Technical Documentation 12 of 25

Page 13: TrakCare User Defined Functions

TrakCare User Defined Functions

msg.AddItem ObjectThe “global” variable msg is created in websys.csp as a new instance of websys.Message (websys.csp is the in-between server page to determine where to go).

When the msg.AddItem(...) is executed in an UDF, the screen reloads back, the message displays and depending on the type of the message, the screen stays or continues in the workflow.

ClassMethod

D msg.AddItem(type,code,description,compid,fieldname,isdirty,jsfunction)

Parameters

type E=Error, W=Warning, I=Information, N=Notification, J=JavaScript code Message code, the code is usually the field that caused the error

description Message description, the description is usually the actual messagecompid compid is the id of the component that the error occurred on, this is

required so we can display the right error message on the correct screen.

fieldname Fieldname is the name of the field where the error occurred on. This is not required, but if entered the screen will display the field with the clsInvalid style (red border around the field to show error)

isdirty <not used>

jsfunction a javascript function name

Types

E Page reloads, message appears with OK button only, user clicks OK, page remainsW page reloads, message appears with OK / Cancel button, if user clicks Cancel page

remains. if user clicks OK, TOVERRIDE set to 1, page gets resubmitted and classmethod is run again, page moves on to next screen

I page reloads, message appears with OK button only, user clicks OK, TOVERRIDE set to 1, page gets resubmitted and classmethod is run again, page moves on to next screen

N page moves on to next screen, message appears with OK button only

J page reloads, message appears with OK / Cancel button, if user clicks Cancel page remains. if user clicks OK, TOVERRIDE set to 1 then jsfunction is called – this jsfunction handles where to proceed from there – stay or move on.

For types W, I, J because the classmethod gets run again, the TOVERRIDE is usually checked again so the message does not appear again (and send it to an infinite loop).

TrakCare Technical Documentation 13 of 25

Page 14: TrakCare User Defined Functions

TrakCare User Defined Functions

OnNewHow to default fields in the OnNew event?

Although it is possible to default fields in an OnNew event it is recommended to use the Layout Editor functionality to default fields.

The below example demonstrates how the Preferred Language and Country of Birth can be defaulted in the OnNew event.

NOTE: the obj variable at this time is used as the structured variable from which the web page is drawn. Consequently, if just before the page is drawn (as in the OnNew event), you pre-populate properties in the obj class, and these values will be inserted onto the page text boxes.

‘obj’ is an instance of the classname specified in the UDF.

Use the GetIdFromCodeOrDescription and SetObjectId methods to populate RowID values into the obj properties. In this example, we are populating default values for Country of Birth, and Preferred Language fields.

UPAPerson ; User defined functions for class PAPerson;

OnNew()new countryid,langid;set ^zTMP("PAPerson","OnNew",1)="Start";default Country of Birth using code "AU"set countryid=##Class(web.CTCountry).GetIdFromCodeOrDescription("UK") if countryid do obj.PAPERCountryBirthDR.SetObjectId(countryid);;default Preferred language using code "ENG"set langid=##Class(web.PACPreferredLanguage).GetIdFromCodeOrDescription("ENG") if langid'="" d obj.PAPERPrefLanguageDR.SetObjectId(langid);set ^zTMP("PAPerson","OnNew",9)="End"quit 1

TrakCare Technical Documentation 14 of 25

Page 15: TrakCare User Defined Functions

TrakCare User Defined Functions

Identifying Components & WorkflowsWhen developing UDFs it is sometimes necessary to identify the exact point of a workflow where specific functionality is required. For example, if a report should only be printed in the PAPerson.Edit component in the Trak.Emergency Header Episode workflow.

How to identify a specific component?To identify a specific component, e.g. the ‘PAPerson.Edit’ component, use the TFORM variable in the %request object. This variable stores the name of the component.

UPAPerson ; User defined functions for class PAPerson;; Called after saving in websysSave

OnAfterSave(id)new component;set component=%request.Get("TFORM")if component="PAPerson.Edit" { ;place component specific logic here}quit 1

How to identify a specific workflow?To identify a specific workflow, e.g. the ‘Trak.Emergency Header Episode’ workflow, use the TWKFL variable in the %request object. This variable stores the id of the workflow. It is therefore necessary to obtain the id from the description using the ‘GetIdFromCodeOfDescription’ method.

UPAPerson ; User defined functions for class PAPerson;; Called after saving in websysSave

OnAfterSave(id)new wkfl;set wkfl=##class(websys.WorkFlow).GetIdFromCodeOrDescription("Trak.Emergency Header Episode")if wkfl=%request.Get("TWKFL") { ;place workflow specific logic here}q 1

How to identify a specific workflow item?To identify a specific workflow item, e.g. the PAPerson.Edit in the ‘Trak.Emergency Header Episode’ workflow, use the TWKFLI variable in the %request object. This variable stores the id of the workflow item. To identify the id of the workflow item see the workflow and if the require workflow item is 3rd in the list then the item id will be 3. See below for further details:

UPAPerson ; User defined functions for class PAPerson;; Called after saving in websysSave

OnAfterSave(id)new wkfl;set wkfl=##class(websys.WorkFlow).GetIdFromCodeOrDescription("Trak.Emergency Header Episode"); identify 3rd item in workflow.if wkfl=%request.Get("TWKFL"),%request.Get("TWKFLI")=3 {

TrakCare Technical Documentation 15 of 25

Page 16: TrakCare User Defined Functions

TrakCare User Defined Functions

;place workflow item specific logic here}quit 1

How to find out workflow ids?Right mouse click on the required workflow and click ‘View Source’.

Using Class ObjectsHow to investigate and update a record using class objects?To retrieve a record using class objects use the %OpenId method with the User package of the TrakCare class.

All properties of a class can be referenced after it is ‘opened’.

Class and property names are the same as the SQL table and field names but without the underscore characters.

Remember to always check if the object exists before use.

Always close the object after use with the %Close methodTrakCare Technical Documentation 16 of 25

Page 17: TrakCare User Defined Functions

TrakCare User Defined Functions

UPAAdm ; User defined functions for class PAAdm;; Called after saving in websysSave

OnAfterSave(id)new objif $get(id)'="" { set obj=##Class(User.PAAdm).%OpenId(id) if obj { set ^zTMP("PAAdmOnAfterSave","RequestMR")=obj.PAADMRequestMR if obj.PAADMRequestMR="Y" { ;perform site specific logic } do obj.%Close() }};quit 1

How to read and update a multi-line field using class objects?

Multi-line fields require extra code to read and to update. See below example:

;The below script gets the multi line field and builds a concatenated string.;new remarks,j,objset obj=##class(User.OEOrdItem).%OpenId(itemid)for j=1:1:obj.OEORIRemarks.Count() {

set remarks=remarks_obj.OEORIRemarks.GetAt(j)_$c(13,10)};

;the below which updates a multi-line field and inserts into database ;new objset obj=##class(User.OEOrdItem).%OpenId(itemid);do obj.OEORIRemarks.Clear()do obj.OEORIRemarks.Insert("Remarks One")do obj.OEORIRemarks.Insert("Remarks Two")do obj.OEORIRemarks.Insert("Remarks Three")do obj.OEORIRemarks.Insert("Remarks Four");do obj.%Save()do obj.%Close()

TrakCare Technical Documentation 17 of 25

Page 18: TrakCare User Defined Functions

TrakCare User Defined Functions

How to use Designative Reference properties using class objects?

A Designative Reference (DR) is a pointer to another table. DRs can also be referred to as foreign keys.

The %Id() method returns the actual DR value, i.e. the rowid value of the other table where the property name will return the id of the object itself.

DR properties can be used as pointers so they can be linked until the required value is found.

UPAPerson ; User defined functions for class PAPerson; ; Called after saving in websysSave

OnAfterSave(id)new obj,zipid,zipobj,cityid,cityobj,countryobj,countrydesckill ^zTMP;if $get(id)'="" { set obj=##Class(User.PAPerson).%OpenId(id) set ^zTMP("PAPerson","OnAfterSave","id")=id ;if the patient has a postcode if obj.PAPERZipDR'="" { ;get zip object set zipobj=obj.PAPERZipDR set ^zTMP("PAPerson","OnAfterSave","ZipObject")=zipobj ; ;get zip id set zipid=obj.PAPERZipDR.%Id() set ^zTMP("PAPerson","OnAfterSave","ZipID")=zipid ; ;link through zip object to get city object set cityobj=obj.PAPERZipDR.CTZIPCITYDR set ^zTMP("PAPerson","OnAfterSave","CityObject")=cityobj ;link through zip object to get city id set cityid=obj.PAPERZipDR.CTZIPCITYDR.%Id() set ^zTMP("PAPerson","OnAfterSave","CityID")=cityid ; ;assuming that all the code tables are set, link from city object to get country description ;if any of the objects are null then an error will occur... set countryobj=cityobj.CTCITProvinceDR.PROVRegionDR.CTRGCountryDR set countrydesc=countryobj.CTCOUDesc set ^zTMP("PAPerson","OnAfterSave","CountryDesc")=countrydesc }};quit 1

TrakCare Technical Documentation 18 of 25

Page 19: TrakCare User Defined Functions

TrakCare User Defined Functions

%session & %request ObjectsHow to use the %session object?

%CSP.Session object contains information about the current session. For example, in TrakCare, this object contains information about the Login User and the Site. You can refer to this object using the object %session. The %session object always includes the following values:

Description Report Parameter

Login Location ID %session.Get("LOGON.CTLOCID")

Site Code %session.Get(“LOGON.SITECODE”)

Login User ID %session.Get("LOGON.USERID")

Login User Code %session.Get(“LOGON.USERCODE”)

Login User Name %session.Get(“LOGON.USERNAME”)

Login Group ID %session.Get(“LOGON.GROUPID”)

Login Group Name %session.Get(“LOGON.GROUPDESC”)

Login Language ID %session.Get(“LOGON.LANGID”)

Client PC IP Address %session.Get("REMOTE_ADDR")

How to use the %request object?%CSP.Request object contains information about the current page. When the CSP Server responds to an HTTP request, it packages up information about the incoming request in an instance of the %CSP.Request object. For example, if a patient is selected, this object contains information like the Patient ID and the Episode ID. You can refer to this object using the variable %request.

To check what values you have available you can right mouse click anywhere on a page and ‘View Source’. For example, if you check the View Source for a Patient Registration screen and scroll down to the hidden value section, you will see the variables that are available and their values.

Report Parameter Examples

Variable Description Report Parameter

PatientID Patient RowId %request.Get(“PatientID”)

EpisodeID Admission RowId %request.Get(“EpisodeID”)

TrakCare Technical Documentation 19 of 25

Page 20: TrakCare User Defined Functions

TrakCare User Defined Functions

Printing, Reporting and EmailingHow to print a report?

Use the ##Class(websys.Report).Print method to print a report. The mandatory parameters are:

REPORTCODE

LOGINLOCATION

IPADDRESSUse the %session object to retrieve the login location and the ip address.

Print Method Parameters

Parameters Description

ReportCode Unique code for the report you want to printLocationID Used in Print Manager to determine the destination printer, e.g. login location

IP Used in Print Manager to determine the destination printer, e.g. client ip addressFilename Name of file when report is exported. Applies to crystal reports and word

documentsUserID Login userid

MailFrom Email address from for emailing reports as attachmentsMailTo Destination email address for emailing reports as attachments

MailCC CC email address for emailing reports as attachmentMailSubject Email subject for emailing reports as attachments

MailMessage Email message for emailing reports as attachmentsDevice Printer device for overriding device returned from Print Manager

NoOfCopies Number of copies to printClassName Inserted in Report History, represents the table that the report is printed against

ClassNameID Inserted in Report History, represent the rowid of the table of the report printedReportHistoryID Requested Report History ID, used for reprinting reports from Report History

ContactMethodType Standard Type used to indicate the type of contact method, i.e. Print, Email, FaxContactRecipient Used to indicate the Referral Doctor recipient from PAC_Refdoctor

Reprint This is set when report is ticked for reprintSaveonly

UPAPerson ; User defined functions for class PAPerson;; Called after saving in websysSaveOnAfterSave(id);

if $get(id)'="" { ;Print Patient Labels

do ##Class(websys.Report).Print("PATLABELS",%session.Get("LOGON.CTLOCID"),%session.Get("REMOTE_ADDR"))};quit 1

TrakCare Technical Documentation 20 of 25

Page 21: TrakCare User Defined Functions

TrakCare User Defined Functions

How to setup parameters for a report?To setup parameters for a report in a UDF set a variable in the %request object. Use the %request.Set function, e.g. d %request.Set(“param1”,”value”) then in the Report Manager use the %request.Get(“param1”) expression to extract the value.

Please be careful not to overwrite any existing variables in the %request object. Check the View Source of the page to check for existing variables.

UPAPerson ; User defined functions for class PAPerson;; Called after saving in websysSaveOnAfterSave(id);

if $get(id)'="" {;Setup parametersSet value=”test”Do %request.Set(“param1”,value)

do ##Class(websys.Report).Print("PATLABELS",%session.Get("LOGON.CTLOCID"),%session.Get("REMOTE_ADDR"))

};quit 1

How to check if a specific report has been printed before?To check if a report has been printed before with the same parameters use the below method:

Do ##Class(websys.PrintHistory).HasPrinted(ReportCode,P1,P2,P3,P4,P5,P6,P7,P8,P9)

Note: P1 to P9 are optional.

UPAPerson ; User defined functions for class PAPerson;; Called after saving in websysSave

OnAfterSave(id)new wkfl,patid;set patid=%request.Get("PatientID")set wkfl=##class(websys.WorkFlow).GetIdFromCodeOrDescription("Trak.Emergency Header Episode");; if in the 3rd Workflow Item of the Trak Emergency Workflowif wkfl=%request.Get("TWKFL"),%request.Get("TWKFLI")=3 { ; if Patient Labels has not been printed before with PatientID being the first and only report

parameter if '(##Class(websys.PrintHistory).HasPrinted("PATIENT_LABELS",patid)) { ; print Patient Labels report do ##Class(websys.Report).Print("PATIENT_LABELS",%session.Get("LOGON.CTLOCID"),

%session.Get("REMOTE_ADDR")) }}quit 1

TrakCare Technical Documentation 21 of 25

Page 22: TrakCare User Defined Functions

TrakCare User Defined Functions

How to print a report if a Print checkbox is checked?To print a report if a print checkbox is ticked, check the value of the hidden field is ’on’.

Example:

UPAPerson ; User defined functions for class PAPerson;; Called after saving in websysSave

OnAfterSave(id) new wkfl,patid

;set patid=%request.Get("PatientID")set wkfl=##class(websys.WorkFlow).GetIdFromCodeOrDescription("Trak.Emergency Header Episode");; Workflow is the Trak Emergency Workflowif wkfl=%request.Get("TWKFL") { ; if Print Patient Letter checkbox is ticked i %request.Get("PrintPatientLetter")="on" { ; print Patient Labels report do ##Class(websys.Report).Print("PATIENT_LETTER",%session.Get("LOGON.CTLOCID"),

%session.Get("REMOTE_ADDR")) }}quit 1

How to save a Crystal Report as a file?Use this method and parameters to save report to file:

Do ##Class(websys.Report).Print(ReportCode, “”, “”, FullFilePathName)

Example:

URBAppointment ;User defined function for class RBAppointment;; Called after saving in websysSave

OnAfterSave(id)new Filename,FilePath,ReportCode;set FilePath="c:\temp"set ReportCode="APPTLETTER";set Filename=FilePath_ReportCode_$job_".rtf"set Filename=FilePath_$job_".rtf";;save crystal report appoinment letter to c:\temp as rtf filedo ##class(websys.Report).Print(ReportCode,"","",Filename);quit 1

TrakCare Technical Documentation 22 of 25

Page 23: TrakCare User Defined Functions

TrakCare User Defined Functions

How to email a message?To e-mail from TrakCare, make sure that smtp server name is set up in Configuration Manager. Use this method and parameters to email a message:

Do ##Class(websys.Mail).JobItem(From,To,CC,Subject,Message,””)

Or

testemail(loop);s (filename,ip)=""s locationid=$o(^CTLOC(0))s ip=$p($p($ZU(131,1),":",2),",",1);s ip=""s userid=$o(^SSU("SSUSR",0)) s filename="testword.rtf"s (MailFrom,MailTo)="[email protected]"i $g(loop)'="" d.f x=1:1:loop d..d ##Class(websys.Report).Print("TESTREPORT",locationid,ip,filename,userid,MailFrom,

MailFrom,"","This is a test for TPS x.xx","message body","","")i $g(loop)="" d

##Class(websys.Report).Print("TESTREPORT",locationid,ip,filename,userid,MailFrom,MailTo,"","This is a test of email for TPS x.xx","message body","","")How to email a message with an attachement?Use this method and parameters to email a message with an attachment:Do ##Class(websys.Mail).JobItem(From,To,CC,Subject,Message,Attachment)

Example:

URBAppointment ;User defined function for class RBAppointment;; Called after saving in websysSave

OnAfterSave(id)New From,To,Subject,CC,Message,crlf,Attachment;; set up email detailsset From="[email protected]"set To="[email protected]"set Subject="Doctor Notification"set CC="[email protected]";; write email messageset crlf=$c(10)_$c(13)set Message="Attention"_crlfset Message=Message_"This is the body"_crlfset Message=Message_"of the email message."_crlfset Message=Message_crlfset Message=Message_"Regards,"_crlfset Message=Message_crlfset Message=Message_"Trakhealth."_crlf;; send email message without attachmentdo ##class(websys.Mail).JobItem(From,To,CC,Subject,Message,"");; send email with attachment – include path and filenameset Attachment="c:\temp\data.txt"do ##class(websys.Mail).JobItem(From,To,CC,Subject,Message,Attachment)

;quit 1

TrakCare Technical Documentation 23 of 25

Page 24: TrakCare User Defined Functions

TrakCare User Defined Functions

How to Print/Email a Word Template?This below code is used to /email saved discharge summary Word templates

testemailword(loop)s locationid=$o(^CTLOC(0))s ip=$p($p($ZU(131,1),":",2),",",1)s filename=$li(^websys.ConfigurationD(1),33)_"\testword.dot"s userid=$o(^SSU("SSUSR",0))i $g(loop)'="" d.f x=1:1:loop d..d

##Class(websys.Report).Print("TestReport",locationid,ip,filename,userid,MailFrom,MailTo,MailCC,MailMailSubject,MailMessage,Device,noofcopies,ClassName,ClassNameID,ReportHistoryID,meth,recip)

i $g(loop)="" d ##Class(websys.Report).Print("TestReport",locationid,ip,filename,userid,MailFrom,MailTo,MailCC,MailSubject,MailMessage,Device,noofcopies,ClassName,ClassNameID,ReportHistoryID,meth,recip)

q

testemailpdf(loop)s locationid=$o(^CTLOC(0))s ip=$p($p($ZU(131,1),":",2),",",1)s filename=$li(^websys.ConfigurationD(1),33)_"\"_$j_".pdf"s userid=$o(^SSU("SSUSR",0))s (MailFrom,MailTo)="[email protected]"i $g(loop)'="" d.f x=1:1:loop d..d ##Class(websys.Report).Print("TESTREPORT",locationid,ip,filename,userid,MailFrom,MailTo,"",

"This is a test of email for TPS 4.22","message body","","")

i $g(loop)="" d ##Class(websys.Report).Print("TESTREPORT",locationid,ip,filename,userid,MailFrom,MailTo,"","This is a test of email for TPS 4.22","message body","","")

q

MethodsSee below for useful methods that are built inside the TrakCare application. This methods can be executed in a Cache Terminal session.

How to determine the Computer Name from IP Address?This method returns the computer name of an IP address using reverse lookup:

w ##Class(websys.Conversions).GetComputerName(ip)

How to determine what the Destination Printer will be from Print Manager?

This method queries the Print Manager and returns the printer device (from the Printer Setup) that will be inserted in the Print History for the print job

w ##Class(websys.Print).GetPrinter(ReportCode,LocationId,ip)

TrakCare Technical Documentation 24 of 25

Page 25: TrakCare User Defined Functions

TrakCare User Defined Functions

Language ReferenceLists all the methods/functions that were used in the above examples.

GetIdFromCodeOrDescriptionThis method returns the RowId from either the Code or the Description. It is an inherited class method that exists in all classes in the web package.

SetObjectIdThis method sets the value for all Designative Reference properties. It is an inherited class method that can be used for all properties in the User package that references another class. That is, all fields that point to other tables.

TrakCare Technical Documentation 25 of 25