Hi Bruce,
I've been on that "fishing trip" you recommended. The following code is what I have come up with. It is in the ServiceMethod Routine embed:
Ins:Institution_ID = Enr:Institution_ID
If Access:Institution.Fetch(Ins:InstID_key) = Level:Benign
Set(Enr:PatID_key, Enr:PatID_key)
Access:PatientEnrollment.Fetch(Enr:PatID_key)
Enr:Study_ID = 'Z'&Enr:Institution_ID&'-'&Enr:Patient_ID
Access:PatientEnrollment.Update()
ELSE
Access:PatientEnrollment.DeleteRecord(0)
p_web.AddServiceError(999, '', '', 'Inst ID Not found', '')
END
To explain, the main goal is to use the API to do an insert on the PatientEnrollment table, and generate the Enr:Study_ID, which is passed back to the client.
The main problem here is the Enr:Patient_ID has an autoincrement key. It seems the new autoincremented value of the Enr:Patient_ID is not available quickly enough during an insert, so it can be used in the construction of the Enr:Study_ID field.
For example, I set the passed in Enr:Institution_ID to 66, which is a valid ID in my test data, and the last Enr:Patient_ID is 1234. After a successful Insert, I would expect the Enr:Study_ID to be: Z66-1235. However I get Z66-0. Note that the insert seems to have autoincremented the Patient_ID properly as the correct value of 1235 is found in the PatientEnrollment table.
So, my solution was to allow this partial record...that is a partial Study_ID...to be inserted and then I immediately open this record and update it with the properly constructed Study_ID.
The other problem I found is, when I pass an invalid Institution_ID, the API does return the proper error message I set in the above code. However, the API still inserts a PatientEnrollment table record for this invalid Institution_ID. I have not been able to conditionally prevent the insert, so again I delete the invalid record after the insert.
To summarize, I'm still looking for a solution to get the next auto-incremented value of Patient_ID sooner. Also, looking for a way to conditionally prevent the insert when the passed institution_ID is invalid. My current solution of updating and deleting does give me the final results I'm looking for but it does seem a bit messy.
Thanks,
Jeff