Compile function TW_Use_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler
$Insert SERVICE_SETUP
$Insert LOGICAL
$Insert TW_USE_EQUATES

Declare subroutine Error_Services, Delay, Update_Index

Equ NUM_ATTEMPTS$ to 10

GoToService

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service VerifyRelationalIndexes(TWUseKey)
    ErrorMsg = ''
    If TWUseKey NE '' then
		RDSTestKey = Field(TWUseKey, '*', 1, 1) 
		If RDSTestKey NE '' then
			// Add index transaction to update TW_USE relational index (target RDS_TEST table)
			If RowExists('TW_USE', TWUseKey) then 
				IndexTransactionRow = 'RDS_TEST*TW_USE_ID*AR':@FM:TWUseKey:@FM:"":@FM:RDSTestKey:@FM
			end else
				IndexTransactionRow = 'RDS_TEST*TW_USE_ID*AR':@FM:TWUseKey:@FM:RDSTestKey:@FM:"":@FM
			end
			Done = False$
			For AttemptNo = 1 to NUM_ATTEMPTS$
				If AttemptNo GT 1 then Delay(1)
				Open "!TW_USE" to BangTable then
					Lock BangTable, 0 then
						Read PendingTrans from BangTable, 0 else PendingTrans = '0':@FM
						PendingTrans := IndexTransactionRow
						Write PendingTrans on BangTable, 0 then
							Done = True$ 
						end else
							If AttemptNo GE NUM_ATTEMPTS$ then
								ErrorMsg = 'Error in ':Service:' service. Unable to write index transaction to !TW_USE. ':TWUseKey
							end
						end
						Unlock BangTable, 0 else ErrorMsg = 'Error in ':Service:' service. Unable to Open !TW_USE to add index transaction. ':TWUseKey
					end else
						If AttemptNo GE NUM_ATTEMPTS$ then
							ErrorMsg = 'Error in ':Service:' service. Unable to Lock !TW_USE to add index transaction. ':TWUseKey
						end
					end
				end else
					If AttemptNo GE NUM_ATTEMPTS$ then
						ErrorMsg = 'Error in ':Service:' service. Unable to Open !TW_USE to add index transaction. ':TWUseKey
					end
				end
			Until Done or ErrorMsg
			Next AttemptNo
			If Done then
				ErrCode = ''
				Update_Index('TW_USE', 'MET_NO', False$, True$)
				If Get_Status(ErrCode) then
					ErrorMsg = 'Error in ':Service:' service. Update_Index call failed. Error code: ':ErrCode
				end
			end
		end
    end
    
    If ErrorMsg NE '' then Error_Services('Add', ErrorMsg)
    
End Service

