# Updating an IFieldInstance

**URL:** https://discourse.geta12.com/t/updating-an-ifieldinstance/1262
**Category:** Kernel
**Tags:** 2021-06
**Created:** [February 6, 2023, 3:57pm UTC](https://discourse.geta12.com/t/updating-an-ifieldinstance/1262 "2023-02-06T15:57:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jan-static-daemon](https://discourse.geta12.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jan-static-daemon](https://discourse.geta12.com/u/jan-static-daemon)
#### Post date: [February 6, 2023, 3:57pm UTC](https://discourse.geta12.com/t/updating-an-ifieldinstance/1262/1 "2023-02-06T15:57:49Z")

</div>

Moin everybody,

I want to update an `IFieldInstance` in a document, and I have question on how documents are initialized. During debugging I’ve observed, that some entity instances are present in my document, filled with null values.

My code to update an `IFieldInstance`:

```auto
    @Override
	public <T> void findFirstFieldInstanceSetValue(IDocument document, String path, T value) {
		document.getEntityInstances().stream()
		        .filter(ei -> ei.getPath().equals(path))
		        .map(IFieldInstance.class::cast)
		        .findFirst()
		        .ifPresent(fi -> fi.setValue(value));
	}

```

Should we **always** check if the field instance exists in the document, and if not create it with `IDocumentFactory#createFieldInstance` and use `IDocument#addEntityInstance` to add it to the document?  
What is the recommended way, in order to update a field instance in a document?

---

<div class="post-metadata">

### Author: ![gildardo-cached-canyon](https://discourse.geta12.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gildardo-cached-canyon](https://discourse.geta12.com/u/gildardo-cached-canyon)
#### Post date: [February 10, 2023, 10:24am UTC](https://discourse.geta12.com/t/updating-an-ifieldinstance/1262/2 "2023-02-10T10:24:22Z")

</div>

Hi @jan-static-daemon,  
what a document contains depends on how you initialize it:

- If you create it manually via `IDocumentFactory`, the document only contains what you add to it via `IDocument#addEntityInstance`.
- If you deserialize an existing document, what it contains depends on how you configure the deserialization process: with or wihout transient fields, with or without empty entity instances.

The best practice (the recommended way) while updating a `IFieldInstance` is how you describe it:

- If it exists, update it
- If not, create it and add it → Here, make sure you are using fitting implementations of `IDocumentFactory`, `IDocument` and co.

**Importante note:** While checking if an `IEntityInstance` exists in a document, it is recommended to consider also the _repetitions_, especially if you are have repeatable groups in your document model.

---

<div class="post-metadata">

### Author: ![jan-static-daemon](https://discourse.geta12.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jan-static-daemon](https://discourse.geta12.com/u/jan-static-daemon)
#### Post date: [February 12, 2023, 9:03am UTC](https://discourse.geta12.com/t/updating-an-ifieldinstance/1262/3 "2023-02-12T09:03:16Z")

</div>

Moin @gildardo-cached-canyon,  
thank you very much, this helps a lot! 🙂
