By code add FieldTemplateFieldGroup to existing ProductFieldTemplate

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! :smiley: 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

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

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…

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:

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);
    }
}
1 Like

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.

1 Like