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 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 they don’t 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 a 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 usually 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 that,

  • 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 wont create a new patient, it will associate the Observation with the existing Patient.

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

In case there are multiple matches for the search, the server returns a 412 Precondition Failed error, indicating that the search criteria were not selective 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 both Patient and Observation were successfully created.

Comments (0)

No comments yet. Be the first to comment!