Index: Scheduling/trunk/m/BSDX08.m
===================================================================
--- Scheduling/trunk/m/BSDX08.m	(revision 1472)
+++ Scheduling/trunk/m/BSDX08.m	(revision 1479)
@@ -1,3 +1,3 @@
-BSDX08	; VW/UJO/SMH - WINDOWS SCHEDULING RPCS ; 7/5/12 12:39pm
+BSDX08	; VW/UJO/SMH - WINDOWS SCHEDULING RPCS ; 7/9/12 4:22pm
 	;;1.7T1;BSDX;;Jul 06, 2012;Build 18
 	; 
@@ -37,5 +37,5 @@
 	Q
 	;
-APPDEL(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)	       ;EP
+APPDEL(BSDXY,BSDXAPTID,BSDXTYP,BSDXCR,BSDXNOT)	       ; Private EP
 	;Called by RPC: BSDX CANCEL APPOINTMENT
 	;Cancels existing appointment in BSDX APPOINTMENT and 44/2 subfiles
@@ -65,20 +65,20 @@
 	S ^BSDXTMP($J,BSDXI)="T00100ERRORID"_$C(30)
 	;
+	; Turn off SDAM APPT PROTOCOL BSDX Entries
+	N BSDXNOEV
+	S BSDXNOEV=1 ;Don't execute BSDX CANCEL APPOINTMENT protocol
+	;
+	;;;test for error inside transaction. See if %ZTER works
+	I $G(BSDXDIE1) N X S X=1/0
+	;
+	; Check appointment ID and whether it exists
+	I '+BSDXAPTID D ERR(BSDXI,"-2~BSDX08: Invalid Appointment ID") Q
+	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"-3~BSDX08: Invalid Appointment ID") Q
+	; 
 	; Lock BSDX node, only to synchronize access to the globals.
 	; It's not expected that the error will ever happen as no filing
 	; is supposed to take 5 seconds.
-	L +^BSDXAPPT(BSDXAPTID):5 I '$T D ERR(BSDXI,"-1~BSDX08: Appt record is locked. Please contact technical support.") Q
-	;
-	; Turn off SDAM APPT PROTOCOL BSDX Entries
-	N BSDXNOEV
-	S BSDXNOEV=1 ;Don't execute BSDX CANCEL APPOINTMENT protocol
-	;
-	;;;test for error inside transaction. See if %ZTER works
-	I $G(BSDXDIE1) N X S X=1/0
-	;
-	; Check appointment ID and whether it exists
-	I '+BSDXAPTID D ERR(BSDXI,"-2~BSDX08: Invalid Appointment ID") Q
-	I '$D(^BSDXAPPT(BSDXAPTID,0)) D ERR(BSDXI,"-3~BSDX08: Invalid Appointment ID") Q
-	; 
+	L +^BSDXAPPT(BSDXAPTID):5 E  D ERR(BSDXI,"-1~BSDX08: Appt record is locked. Please contact technical support.") Q
+	;
 	; Start Processing:
 	; First, get data
@@ -124,13 +124,12 @@
 	; BSDXAPPT First; no need for rollback if error occured.
 	N BSDXERR S BSDXERR=$$BSDXCAN(BSDXAPTID)  ; Add a cancellation date in BSDX APPOINTMENT
-	I BSDXERR D ERR(BSDXI,"$$BSDXCAN failed (Fileman filing error): "_$P(BSDXERR,U,2)) QUIT
+	I BSDXERR D ERR(BSDXI,"-10~BSDX08: $$BSDXCAN failed (Fileman filing error): "_$P(BSDXERR,U,2)) QUIT
 	;
 	; Then PIMS: 
 	; cancel appt in "S" nodes in file 2 and 44, then update Legacy PIMS Availability
 	; If error happens, must rollback ^BSDXAPPT
-	I BSDXLOC D  QUIT:BSDXERR
-	. S BSDXERR=$$CANCEL^BSDXAPI(.BSDXC) ; Cancel through BSDXAPI
-	. ; Rollback BSDXAPPT if error occurs
-	. I BSDXERR D ERR(BSDXI,"-9^BSDX08: BSDXAPI returned an error: "_$P(BSDXERR,U,2)),ROLLBACK(BSDXAPTID)  QUIT
+	I BSDXLOC S BSDXERR=$$CANCEL^BSDXAPI(.BSDXC) ; Cancel through BSDXAPI
+	; Rollback BSDXAPPT if error occurs
+	I BSDXERR D ERR(BSDXI,"-9^BSDX08: BSDXAPI returned an error: "_$P(BSDXERR,U,2)),ROLLBACK(BSDXAPTID)  QUIT
 	;
 	L -^BSDXAPPT(BSDXAPTID)
@@ -186,5 +185,5 @@
 	. S BSDXNOD=$G(^BSDXAPPT(BSDXAPPT,0)) Q:BSDXNOD=""
 	. I $P(BSDXNOD,U,5)=BSDXPAT,$P(BSDXNOD,U,12)="" S BSDXFOUND=1 Q
-	I BSDXFOUND,+$G(BSDXAPPT) D BSDXCAN(BSDXAPPT)
+	I BSDXFOUND,+$G(BSDXAPPT) N % S %=$$BSDXCAN(BSDXAPPT) I % D ^%ZTER
 	Q BSDXFOUND
 	;
@@ -201,4 +200,6 @@
 	;
 ERR(BSDXI,BSDXERR)	;Error processing
+	; Unlock first
+	L:$D(BSDXAPTID) -^BSDXAPPT(BSDXAPTID)
 	; If last line is $C(31), we are done. No more errors to send to client.
 	I ^BSDXTMP($J,$O(^BSDXTMP($J," "),-1))=$C(31) QUIT
@@ -208,5 +209,4 @@
 	S BSDXI=BSDXI+1
 	S ^BSDXTMP($J,BSDXI)=$C(31)
-	L -^BSDXAPPT(BSDXAPTID)
 	QUIT
 	;
@@ -214,8 +214,10 @@
 	N $ET S $ET="D ^%ZTER HALT"  ; Emergency Error Trap
 	D ^%ZTER
+	;
 	; Roll back BSDXAPPT; 
 	; NB: What if a Mumps error happens inside fileman in BSDXAPI? 
 	; I have decided the M errors are out of scope for me to handle.
 	D:$G(BSDXAPTID) ROLLBACK(BSDXAPTID)
+	;
 	; Log error message and send to client
 	I '$D(BSDXI) N BSDXI S BSDXI=0
