# MutliField add/delete fields by code

**URL:** <https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515>\
**Category:** Questions\
**Tags:** litium-7\
**Created:** [June 2, 2020, 12:41pm UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515 "2020-06-02T12:41:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon.Halling](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Simon.Halling](https://forum.litium.com/u/Simon.Halling)\
**Post date:** [June 2, 2020, 12:41pm UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/1 "2020-06-02T12:41:35Z")

</div>

Hi!

When modifying (adding/removing) fields from the MultiField (by code), I allways have to add/remove the Multifield in the BackOffice in order for it to appear. Is it to possible to update a MultiField by code?

I usually solve it by deleting the MultiField from the Backoffice and then build the solution…or go info the backoffice (manually update)…But it’s a struggle when having multiple environments.

Thankful for any answers 🙂

Litium version: 7.4.2

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [June 2, 2020, 1:48pm UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/2 "2020-06-02T13:48:15Z")

</div>

Standard behavior is that templates and fields defined in code are only created/updated during startup if they are new ([read more](https://github.com/LitiumAB/Education/tree/master/Developer%20Education/Tasks/Author%20page#if-your-template-is-incorrect-and-needs-to-be-re-generated)).

You can modify `DefinitionSetup.InitFields()` to always update fields from code on startup, will require adding a call to `_fieldDefinitionService.Update()`

I have only tested this with templates myself and not with fields so I hope it works, try it out and post here on how it goes.

---

<div class="post-metadata">

**Author:** ![Simon.Halling](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Simon.Halling](https://forum.litium.com/u/Simon.Halling)\
**Post date:** [June 2, 2020, 2:11pm UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/3 "2020-06-02T14:11:08Z")

</div>

Thanks for the input.

Unfortunately it did not work. Added following line in the InitFields() function.  
\_fieldDefinitionService.Update(currentField);

Did Build and Rebuild the solution

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [June 2, 2020, 2:15pm UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/4 "2020-06-02T14:15:50Z")

</div>

Did you also remove the check if the field was already created?

---

<div class="post-metadata">

**Author:** ![Simon.Halling](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Simon.Halling](https://forum.litium.com/u/Simon.Halling)\
**Post date:** [June 2, 2020, 2:49pm UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/5 "2020-06-02T14:49:13Z")

</div>

If you mean this one, then yes:

if (IsAlreadyExecuted(item.Id, item.AreaType.Name))  
{  
//continue;  
}

---

<div class="post-metadata">

**Author:** ![marten](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/marten/32/11_2.png) [@marten](https://forum.litium.com/u/marten)\
**Post date:** [June 2, 2020, 5:33pm UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/6 "2020-06-02T17:33:07Z")

</div>

> [@Simon.Halling](#):
>
> \_fieldDefinitionService.Update(currentField);

Noticed that you seem to be updating using the old field and not your new definition, try changing to:

```auto
item.SystemId = currentField.SystemId;
_fieldDefinitionService.Update(item);

```

---

<div class="post-metadata">

**Author:** ![Simon.Halling](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Simon.Halling](https://forum.litium.com/u/Simon.Halling)\
**Post date:** [June 3, 2020, 7:11am UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/7 "2020-06-03T07:11:04Z")

</div>

Thanks marten, that did the job 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [July 1, 2020, 7:11am UTC](https://forum.litium.com/t/mutlifield-add-delete-fields-by-code/1515/8 "2020-07-01T07:11:05Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
