// This module is included in the following assemblies:
// as_connecting-to-fhir.adoc

[id='adding-fhir-connection-patch_{context}']
= Updating specified fields in a resource on a FHIR server

In an integration, you can update individual fields in 
a resource that is on a FHIR server.  
To do this, add a FHIR connection to the middle of a flow
or as a simple integration's finish connection. 

.Prerequisites
* You created a connection to the FHIR server that has the 
resource that you want to update.
* You are creating or editing a flow and {prodname} is prompting you
to add to the integration. Or, {prodname} is prompting you to choose a finish connection. 

.Procedure
. On the *Add to Integration* page, click the plus sign where you 
want to add the connection. Skip this step if {prodname} is
prompting you to choose the finish connection. 
. Click the FHIR connection that you want to use
to update the resource. 
. On the *Choose an action* page, select *Patch*. 
. Click in the *Resource Type* field to display a list
of FHIR resource types. Select or enter the type of the 
resource whose fields you want to update. 
. In the *Number of Operations* field, specify the number of fields
that you want to update. 
. Leave the *Resource id* field blank if you want to map the value
from a previous step in the flow, which is typically what you want 
to do. Otherwise, specify the resource 
ID of the resource whose fields you want to update.  

. Specify a JSON patch that describes the updates. 
The JSON patch must have the format described in 
link:http://jsonpatch.com/[What is a JSON Patch?].
+
*Examples of patches for a `Patient` resource*: 
+
Set the `active` field to `true`:
+
----
[{ "op":"replace", "path":"/active", "value": true }]
----
+
Replace the value of the `name` field, which is an array of the patient’s names. 
In this example, the patient has one name. If the patient had more than one 
name, a similarly formatted patch could replace all of the patient’s names:
+
----
[{ "op":"replace", "path":"/name", "value": [{"given": ["Bob"]}] }]
----
+
Replace the given name of a patient:
+
----
[{ "op":"replace", "path":"/name/0/given/0", "value": "John" }]
----
+
Add another name to a patient before the first name:
+
----
[{ "op":"add", "path":"/name/0", "value": {"given": ["Anthony"]} }]
----
+
For some updates, you must specify the JSON patch when you 
configure the *Patch* action. For example, if you want to copy the 
value of a resource field to another field. For other updates, 
you can leave the *JSON Patch* field blank and instead map values 
that define each update.  
. Click *Next* to add this connection to the flow. 
. If you did not specify a JSON patch and/or a resource ID, then 
add a data mapper step:
.. In the flow visualization, click the plus sign that is
before the FHIR connection that you just added. 
.. Click *Data Mapper*. 
+
The *Target* panel displays a numbered folder for each field 
that you want to update. When you configured the *Patch* action, 
you specified how many fields to update. The data mapper 
displays this number of folders in the *Target* panel. For example, 
if you specified 3 as the number of fields to update, you would see 
three target folders with the labels `1`, `2`, and `3`. 

.. For each field that you want to update, map to the fields 
in one target folder:

... In the *Target* panel, expand a folder to display three fields 
for `op`, `path`, and `value`.

... Map a source field, constant, or property to the target 
`path` field. The path identifies the resource field 
that you want to update. In a path value, a number indicates the 
index of a list field, and a slash leads to a child field. For 
example, map this path `/name/1/given/1/value` to update the value 
of the given name field. 

... Accept the default update operation, which replaces the current value
of the field, or map a source field, constant, or property to the 
`op` field to indicate how you want to update the field. 
For details about possible operations, see 
link:ttp://jsonpatch.com/#operations[JSON patch operations].
... If you are updating a field to have a new value, then map a 
source field, constant, or property to the target `value`. 
This is the new value that you want the field to contain. 

.. If you did not specify a resource ID when you configured the 
*Patch* action, then map the resource ID to the target *id* field. 

.. Click *Done* to add the data mapper step to the flow. 

.Result
The connection appears in the integration visualization
where you added it. During execution, 
the connection updates the specified resource field(s)
and returns a *MethodOutcome* resource that includes 
an `id.idPart` field. This field contains 
the ID of the resource that was updated. 
