Updating an IFieldInstance

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:

    @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?

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.

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