Skip to content

Transaction Bundle Examples

Use transaction bundles to create or modify multiple resources atomically.

Basic Transaction

Define the bundle as a fixture:

fixtures/patient-with-org.json
{
  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [
    {
      "fullUrl": "urn:uuid:org-1",
      "resource": {
        "resourceType": "Organization",
        "name": "Example Hospital"
      },
      "request": {
        "method": "POST",
        "url": "Organization"
      }
    },
    {
      "fullUrl": "urn:uuid:patient-1",
      "resource": {
        "resourceType": "Patient",
        "name": [{ "family": "Smith", "given": ["John"] }],
        "managingOrganization": { "reference": "urn:uuid:org-1" }
      },
      "request": {
        "method": "POST",
        "url": "Patient"
      }
    }
  ]
}

Use it in a TestScript:

{
  "fixture": [
    {
      "id": "patient-with-org",
      "autocreate": false,
      "autodelete": false,
      "resource": {
        "reference": "fixtures/patient-with-org.json"
      }
    }
  ],
  "setup": {
    "action": [
      {
        "operation": {
          "type": { "code": "transaction" },
          "sourceId": "patient-with-org",
          "responseId": "transactionResponse"
        }
      },
      {
        "assert": {
          "description": "Transaction succeeded",
          "response": "okay",
          "warningOnly": false
        }
      }
    ]
  }
}

Extract IDs from Transaction Response

Use FHIRPath to extract the server-assigned IDs from the transaction response bundle:

{
  "variable": [
    {
      "name": "patientId",
      "expression": "Bundle.entry.where(response.location.contains('Patient')).response.location.replaceMatches('.*/Patient/([^/]+).*', '$1')",
      "sourceId": "transactionResponse"
    }
  ]
}

Conditional Transaction Entries

Use ifNoneExist to avoid duplicate creates:

{
  "entry": [
    {
      "fullUrl": "urn:uuid:practitioner-1",
      "resource": {
        "resourceType": "Practitioner",
        "identifier": [
          {
            "system": "http://ns.electronichealth.net.au/id/hi/hpii/1.0",
            "value": "8003610833334085"
          }
        ]
      },
      "request": {
        "method": "POST",
        "url": "Practitioner",
        "ifNoneExist": "identifier=http://ns.electronichealth.net.au/id/hi/hpii/1.0|8003610833334085"
      }
    }
  ]
}

Assert Transaction Response

{
  "assert": {
    "description": "All entries succeeded",
    "expression": "Bundle.entry.response.status.all(startsWith('200') or startsWith('201'))",
    "warningOnly": false
  }
}

Batch vs Transaction

Feature Transaction Batch
Atomicity All or nothing Independent entries
On failure All rolled back Failed entries noted
Use for Related data setup Independent operations
FHIR type transaction batch

Next Steps