On this page:

32.12Composition Resource API


The Composition resource in the JavaScript Execution Environment has some additional functionality in addition to that already available for any resource.

To create a Composition resource, use ResourceBuilder.build('Composition').

32.12.1Method: set*(value)


There are several setters for basic fields of a Composition.


  • value: String – the value to set the field to. All setters take a single string argument.

Valid setter methods:

  • setSubject
  • setStatus
  • setEncounter
  • setCustodian
  • setAuthor (removes all other added authors)

Instead of setAuthor, you can use addAuthor to non-destructively add an author.


  • Populates the field with the requested value
  • Returns the same Composition object for chaining.


var composition = ResourceBuilder.build('Composition');


var composition = ResourceBuilder.build('Composition');

// Example 1: Set the subject by Patient Name
var patients = Fhir
    .where('given', 'Ron')
    .where('family', 'Howard')

// Example 2: Set the Composition subject to the subject of an Observation
var observation = Fhir.read('Observation/123');

32.12.2Method: setDate(epoch)


This method sets the date of the composition.


  • epoch – the unix timestamp to set the date of the composition to. If you have a Date object date, you can use date.getTime() to retrieve the timestamp


  • Sets the datetime on the composition
  • Returns the same Composition object for chaining.
var composition = ResourceBuilder.build('Composition');

var date = new Date();
var epoch = date.getTime();


32.12.3Method: setType(documentType)


This method can be used to auto-populate the type field with the correct coding for a C-Cda document.


  • documentType – the c-cda template this composition represents.

The definitive list of document types can be found in the CDA Exchange Module available types and mappings section.


  • Populates the Composition.type field with an appropriate Coding, including display name.
  • Returns the same Composition object for chaining.

Example usage:

var composition = ResourceBuilder.build('Composition');

Note: to set a custom document type, populate the appropriate fields using the ResourceBuilder API:

var composition = ResourceBuilder.build('Composition');
composition.type.coding.system = "My custom system";
composition.type.coding.code= "My custom code";
composition.type.coding.display = "My custom display";

32.12.4Method: addSection(sectionType)


This method can be used to create a new section and auto-populate its code and title appropriately for a given C-CDA section template.


  • documentType – the c-cda section-level template this section represents.

The definitive list of available sections can be found in the CDA Exchange Module available types and mappings section.

var composition = ResourceBuilder.build('Composition');
var allergySection = composition.addSection('allergyintolerance');


  • Returns a CompositionSectionBuilder resource to work with.

Note: to set custom section details, use CompositionSectionBuilder.setCode(system, code, display) or populate the appropriate fields using the ResourceBuilder API on the Composition (not on the section):

var composition = ResourceBuilder.build('Composition');
var section = composition.addSection();
composition.section[0].code.coding.system = "my custom system";
composition.section[0].code.coding.code = "my custom code";
composition.section[0].code.coding.display = "my custom display";