Populating Persistent Class from JSON
As I am iterating through the FHIR JSON Response from a Patient Search, I am running into an issue where the extracted values are not being popualted into the Response object that I have created. If I do a $$$LOGINFO on them they will show up in the Object log, however if I try the following for example I get an error.
set target.MRN = identifier.value
|
Here is my Ens.DataTransform
Class osuwmc.Epic.FHIR.DTL.FHIRResponseToPatient Extends Ens.DataTransform
{
ClassMethod Transform(source As HS.FHIRServer.Interop.Response, target As osuwmc.Epic.FHIR.DataStructures.PatientSearch.Response) As %Status
{
Set tSC=$$$OK
set tQuickStream = ##Class(HS.SDA3.QuickStream).%OpenId(source.QuickStreamId)
set tRawJSON = ##Class(%Library.DynamicObject).%FromJSON(tQuickStream)
$$$TRACE(tRawJSON.%ToJSON())
set tResource = tRawJSON.entry.%Get(0).resource
if tResource.resourceType '= "Patient" {
set tSC = $$$ERROR($$$GeneralError, "FHIRResponseToPatient: Resource type is not Patient")
return tSC
}
else{
$$$LOGINFO("Resource Type: "_tResource.resourceType)
set mrnIter = tResource.identifier.%GetIterator()
while mrnIter.%GetNext(,.identifier) {
if identifier.system = "urn:oid:1.2.840.114350.1.13.172.2.7.5.737384.100" {
set target.MRN = identifier.value
}
}
set NameIter = tResource.name.%GetIterator()
while NameIter.%GetNext(,.humanName) {
if humanName.use = "official" {
set target.LastName = humanName.family
set target.FirstName = humanName.given.%Get(0)
}
}
set target.DOB = tResource.birthDate
set target.Gender = tResource.gender
set addrIter = tResource.address.%GetIterator()
while addrIter.%GetNext(,.address) {
if address.use = "home" {
set target.Address = address.line.%Get(0)
set target.City = address.city
set target.State = address.state
set target.PostalCode = address.postalCode
}
}
}
return tSC
}
}
If I replace the set statements with $$$LOGINFO, the values will show up in the trace viewer.
Comments
The issue you're encountering with <INVALID OREF> suggests that the identifier.value or target.MRN is not a valid ObjectScript reference (OREF) during the operation. This happens if the identifier is not properly initialized as an object. To address this issue, verify that identifier contains a valid OREF before trying to access its property value. You can do this using $ISOBJECT.
Here’s an example adjustment to your code:
while mrnIter.%GetNext(,.identifier) {
if $ISOBJECT(identifier) && (identifier.system = "urn:oid:1.2.840.114350.1.13.172.2.7.5.737384.100") {
set target.MRN = identifier.value
} else {
$$$LOGINFO("Invalid OREF for identifier or system mismatch")
}
}
This ensures the loop only processes identifier objects that are valid and avoids errors when working with null or invalid OREFs [1][2][3].
Sources:
Are you instantiating the 'target' variable?...
It doesn't look like it, hence your Invalid Oref when trying to access the MRN property (unless you're sending it in ByRef, but in that case it seems not be an object either).
So it looks like you were focused on the right side of your SET command, though the problem might be on the left side.
The invalid oref error because of the target object "osuwmc.Epic.FHIR.DataStructures.PatientSearch.Response" not initiated. instantiating the target if it's not OREF.
If '$IsObject(target){
Set target = ##class(osuwmc.Epic.FHIR.DataStructures.PatientSearch.Response).%New()
}'