Conditional Create

What if you want to post data to a FHIR server, but only create a new resource if it doesn’t already exist?

This is a common use case in FHIR, for example when working with wearable devices or remote monitoring systems that continuously send observations to a server. Without safeguards, this can lead to the creation of duplicate Patient records.

The best way to implement this is by using conditional create, which ensures that you don’t create duplicate resources while still associating new observations with existing resources.

In this lesson we will:

  1. Create a Patient conditionally if it doesn’t already exist
  2. Associate an Observation with the Patient
  3. Handle a scenario where the Patient already exists

Setting up conditional create

A conditional create uses a POST request with an ifNoneExist parameter.

  • If the server finds a Patient matching the search condition (e.g. the same identifier), it reuses that resource
  • If not, it will create a new Patient.

This is typically done with a transaction bundle so that related resource creation is done atomically.

Here is an example transaction type bundle that is prepared for executing conditional create.

{
 "resourceType": "Bundle",
 "type": "transaction",
 "entry": [
   {
     "fullUrl": "urn:uuid:patient-126",
     "resource": {
       "resourceType": "Patient",
       "identifier": [
         { "value": "123-123-126" }
       ],
       "name": [{
           "given": ["Sidharth"],
           "family": "Ramesh" }],
       "telecom": [{
           "system": "email",
           "value": "sidharth@medblocks.com" }]
     },
     "request": {
       "method": "POST",
       "url": "Patient",
       "ifNoneExist": "identifier=123-123-126"
     }
   },
   {
     "resource": {
       "resourceType": "Observation",
       "status": "final",
       "code": {
         "coding": [
           {
             "system": "http://loinc.org",
             "code": "4548-4",
             "display": "Hemoglobin A1c/Hemoglobin.total in Blood"
           }
         ]
       },
       "valueQuantity": {
         "value": 6.3,
         "unit": "%",
         "system": "http://unitsofmeasure.org",
         "code": "%"
       },
       "subject": { "reference": "urn:uuid:patient-126" }
     },
     "request": { "method": "POST", "url": "Observation" }
   }
 ]
}

Note the following key points:

  • ifNoneExist tells the server to check for an existing Patient with the same identifier before creating a new one
  • urn:uuid:patient-126 is a temporary reference that ensures that the Observation links correctly to the Patient, new or existing.

What happens if the Patient already exists?

If a Patient matching the identifier already exists, the server will not create a new patient. Instead, it will associate the Observation with the existing Patient.

On posting this bundle to the server, we will get a 200 OK response, indicating that the Observation was successfully added to an existing Patient.

In case there are multiple matches for the search, the server will return a 412 Precondition Failed error, indicating that the search criteria were not specific enough.

What if the Patient doesn’t exist?

In this case, the server will first create the Patient. Then it will associate the Observation with the newly created Patient.

We will receive a 201 Created response, indicating that both the Patient and the Observation were successfully created.

Summary

In this lesson we explored how to conditionally create resources in FHIR, to ensure that a new resource is created only if it doesn’t already exist on the server. We explored how to use the ifNoneExist parameter in a POST request to prevent duplicate Patient records. We also covered how to associate Observations with existing Patients, and how it works when the Patient exists or doesn’t exist.

Comments (0)

No comments yet. Be the first to comment!