# By code add FieldTemplateFieldGroup to existing ProductFieldTemplate

**URL:** https://forum.litium.com/t/by-code-add-fieldtemplatefieldgroup-to-existing-productfieldtemplate/863
**Category:** Questions
**Tags:** litium-6, accelerator
**Created:** [May 16, 2019, 12:36pm UTC](https://forum.litium.com/t/by-code-add-fieldtemplatefieldgroup-to-existing-productfieldtemplate/863 "2019-05-16T12:36:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Cissi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/cissi/32/18_2.png) [@Cissi](https://forum.litium.com/u/Cissi)
#### Post date: [May 16, 2019, 12:36pm UTC](https://forum.litium.com/t/by-code-add-fieldtemplatefieldgroup-to-existing-productfieldtemplate/863/1 "2019-05-16T12:36:30Z")

</div>

I’ve created 50 new fields in the ProductsDefinitionSetup. Now I want all of this field to be added to a existing ProductFieldTemplate. I want to add a FieldTemplateFieldGroup to the VariantFieldGroups and add my new fields. I’ve tried to copy some accelerator code and I’ve tried to do this. But I have failed! 😃 So, how do I do this? I don’t want to add this group and theese fields to all of our environments. I just want it to be created by deploy.  
Someone?

Litium version: 6.3.2-patch-1903221330

---

<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: [May 21, 2019, 7:21am UTC](https://forum.litium.com/t/by-code-add-fieldtemplatefieldgroup-to-existing-productfieldtemplate/863/2 "2019-05-21T07:21:56Z")

</div>

In `ProductsDefinitionSetup.InitTemplates()` there is a check if the template is already created, if it is then the template is not generated again from code so this is only done on first startup. If you have not done it already that code need to be adjusted to update the template on every startup if you always want to overwrite from code.

See also these related questions: [https://forum.litium.com/search?q=common.setting](https://forum.litium.com/search?q=common.setting)

---

<div class="post-metadata">

### Author: ![Cissi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/cissi/32/18_2.png) [@Cissi](https://forum.litium.com/u/Cissi)
#### Post date: [May 21, 2019, 9:11am UTC](https://forum.litium.com/t/by-code-add-fieldtemplatefieldgroup-to-existing-productfieldtemplate/863/3 "2019-05-21T09:11:49Z")

</div>

But can’t I just create a productgroup and add my fields and add it to an existing template without doing that?  
I’ve looked at the code there and I dosent get overwritter anyway. All I want to do is to add some new fields, it’s 50. I don’t want to do it manually…

---

<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: [May 21, 2019, 11:19am UTC](https://forum.litium.com/t/by-code-add-fieldtemplatefieldgroup-to-existing-productfieldtemplate/863/4 "2019-05-21T11:19:27Z")

</div>

The fieldgroup is part of the template and template will not be generated more that first startup in default code.

If you have a Litium 7 Accelerator avaliable you can compare the code to `DefinitionSetup.InitTemplates()` in that version. There the code can easily be modified by commenting out the `IsAlreadyExecuted`-check to create or update the template on every startup:

L7 code example:

```auto
private void InitTemplates(IEnumerable<FieldTemplate> templates)
{
    foreach (var item in templates)
    {
        //if (IsAlreadyExecuted<FieldTemplate>(item.Id, item.AreaType.Name))
        //{
        // continue;
        //}

        var currentField = _fieldTemplateService.Get<FieldTemplate>(item.AreaType, item.Id);
        try
        {
            _fieldFrameworkSetupLocalizationService.Localize(item, ((dynamic)item).FieldGroups as ICollection<FieldTemplateFieldGroup>);
        }
        catch
        {
            _fieldFrameworkSetupLocalizationService.Localize(item);
        }

        if (currentField == null)
        {
            _fieldTemplateService.Create(item);
        }
        else
        {
            item.SystemId = currentField.SystemId;
            _fieldTemplateService.Update(item);
        }

        SetAlreadyExecuted<FieldTemplate>(item.Id, item.AreaType.Name);
    }
}

```

---

<div class="post-metadata">

### Author: ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)
#### Post date: [May 22, 2019, 7:30am UTC](https://forum.litium.com/t/by-code-add-fieldtemplatefieldgroup-to-existing-productfieldtemplate/863/5 "2019-05-22T07:30:50Z")

</div>

And your question was for Litium 6 and not Litium 7 but it is mostly the same. In the `ProductDefinitionSetup` you have a lot of rows that points to the method `IsAlreadyExecuted` that will prohibit updating an already existing field/template/displaytemplate… you need to rewrite the logic that are using this `IsAlreadyExecuted` code to update the template if it already exists instead of creating a new one.
