The BundleBuilder (JavaDoc) can be used to construct FHIR Bundles.
Note that this class is a work in progress! It does not yet support all transaction features. We will add more features over time, and document them here. Pull requests are welcomed.
To add an update (aka PUT) operation to a transaction bundle
// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Create a Patient to create
Patient patient = new Patient();
patient.setActive(true);
// Add the patient as a create (aka POST) to the Bundle
builder.addTransactionCreateEntry(patient);
// Execute the transaction
IBaseBundle outcome =
myFhirClient.transaction().withBundle(builder.getBundle()).execute();
If you want to perform a conditional create:
// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Create a Patient to create
Patient patient = new Patient();
patient.setActive(true);
patient.addIdentifier().setSystem("http://foo").setValue("bar");
// Add the patient as a create (aka POST) to the Bundle
builder.addTransactionCreateEntry(patient).conditional("Patient?identifier=http://foo|bar");
// Execute the transaction
IBaseBundle outcome =
myFhirClient.transaction().withBundle(builder.getBundle()).execute();
To add an update (aka PUT) operation to a transaction bundle:
// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Create a Patient to update
Patient patient = new Patient();
patient.setId("http://foo/Patient/123");
patient.setActive(true);
// Add the patient as an update (aka PUT) to the Bundle
builder.addTransactionUpdateEntry(patient);
// Execute the transaction
IBaseBundle outcome =
myFhirClient.transaction().withBundle(builder.getBundle()).execute();
If you want to perform a conditional update:
// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Create a Patient to update
Patient patient = new Patient();
patient.setActive(true);
patient.addIdentifier().setSystem("http://foo").setValue("bar");
// Add the patient as an update (aka PUT) to the Bundle
builder.addTransactionUpdateEntry(patient).conditional("Patient?identifier=http://foo|bar");
// Execute the transaction
IBaseBundle outcome =
myFhirClient.transaction().withBundle(builder.getBundle()).execute();
To add a PATCH operation to a transaction bundle:
// Create a FHIR Patch object
Parameters patch = new Parameters();
Parameters.ParametersParameterComponent op = patch.addParameter().setName("operation");
op.addPart().setName("type").setValue(new CodeType("replace"));
op.addPart().setName("path").setValue(new CodeType("Patient.active"));
op.addPart().setName("value").setValue(new BooleanType(false));
// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Create a target object (this is the ID of the resource that will be patched)
IIdType targetId = new IdType("Patient/123");
// Add the patch to the bundle
builder.addTransactionFhirPatchEntry(targetId, patch);
// Execute the transaction
IBaseBundle outcome =
myFhirClient.transaction().withBundle(builder.getBundle()).execute();
If you want to perform a conditional patch:
// Create a FHIR Patch object
Parameters patch = new Parameters();
Parameters.ParametersParameterComponent op = patch.addParameter().setName("operation");
op.addPart().setName("type").setValue(new CodeType("replace"));
op.addPart().setName("path").setValue(new CodeType("Patient.active"));
op.addPart().setName("value").setValue(new BooleanType(false));
// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Add the patch to the bundle with a conditional URL
String conditionalUrl = "Patient?identifier=http://foo|123";
builder.addTransactionFhirPatchEntry(patch).conditional(conditionalUrl);
// Execute the transaction
IBaseBundle outcome =
myFhirClient.transaction().withBundle(builder.getBundle()).execute();
If you want to manipulate a bundle:
// Create a TransactionBuilder
BundleBuilder builder = new BundleBuilder(myFhirContext);
// Set bundle type to be searchset
builder.setBundleField("type", "searchset")
.setBundleField("id", UUID.randomUUID().toString())
.setMetaField("lastUpdated", builder.newPrimitive("instant", new Date()));
// Create bundle entry
IBase entry = builder.addEntry();
// Create a Patient to create
Patient patient = new Patient();
patient.setActive(true);
patient.addIdentifier().setSystem("http://foo").setValue("bar");
builder.addToEntry(entry, "resource", patient);
// Add search results
IBase search = builder.addSearch(entry);
builder.setSearchField(search, "mode", "match");
builder.setSearchField(search, "score", builder.newPrimitive("decimal", BigDecimal.ONE));