Skip to main content
Solved

Initial value of Feature Flag


mawe4585
Freshman II

When I create a FeaturesSetExt to introduce a new Feature Flag, the new Field on the database is initialized with NULL.
Even though I set the PXDefault to false (I know this is redundant), I have trouble activating other Features, if my new feature was not activated and is still NULL, I always get the error “’MyFeature’ cannot be empty”.

  public sealed class FeaturesSetExt : PXCacheExtension<FeaturesSet>
  {
    public static bool IsActive() => true;

    #region UsrMyFeature
    [PXDefault(false, PersistingCheck = PXPersistingCheck.Nothing)]  
    [Feature(false, typeof(FeaturesSet.subAccount), DisplayName = "UsrMyFeature" )]
    public bool? UsrMyFeature{ get; set; }
    public abstract class usrMyFeature: BqlBool.Field<usrMyFeature> { }
    #endregion
  }

I solved it by adding an SQL UPDATE script to my customization that initializes the field with False. But it doesn’t look like the best solution.

Do you have ideas on how to solve this?

Best answer by Zoltan Febert

Hi @mawe4585, we usually solve this with a Customization Plugin class like this:

public class MyCustomizationPlugin : Customization.CustomizationPlugin
{
    public override void UpdateDatabase()
    {
        PXDatabase.Update<FeatureSet>(
            new PXDataFieldAssign("usrMyFeature", true),
            new PXDataFieldRestrict("usrMyFeature", PXDbType.VarChar, 1, null, PXComp.ISNULL)
        );

        WriteLog("My Plugin finished working.");
    }
}

 

View original
Did this topic help you find an answer to your question?

5 replies

Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3
  • Jr Varsity I
  • 175 replies
  • Answer
  • May 15, 2024

Hi @mawe4585, we usually solve this with a Customization Plugin class like this:

public class MyCustomizationPlugin : Customization.CustomizationPlugin
{
    public override void UpdateDatabase()
    {
        PXDatabase.Update<FeatureSet>(
            new PXDataFieldAssign("usrMyFeature", true),
            new PXDataFieldRestrict("usrMyFeature", PXDbType.VarChar, 1, null, PXComp.ISNULL)
        );

        WriteLog("My Plugin finished working.");
    }
}

 


mawe4585
Freshman II
  • Author
  • Freshman II
  • 7 replies
  • May 15, 2024

@Zoltan Febert 

I also just solved it with a customizationPlugin. It’s way better than an SQL script, because it only effects the current tenant and also works with new tenants.

 

    public class MyCustomizationPlugin : CustomizationPlugin
    {

        //This method executed after customization was published and website was restarted.  
        public override void UpdateDatabase()
        {
            this.WriteLog("MyCustomization Update Event");
            FeaturesMaint featuresMaint = PXGraph.CreateInstance<FeaturesMaint>();        

            Update<FeaturesSet>.
                Set<FeaturesSetExt.usrMyFeature.EqualTo<False>>.
                Where<FeaturesSetExt.usrMyFeature.IsNull>
            .Update(featuresMaint);
        }
    }

 


darylbowman
Captain II
Forum|alt.badge.img+13
mawe4585 wrote:

 

It’s way better than an SQL script, because it only effects the current tenant...

Is there a reason you would only want to affect the current tenant? As long as you’re only setting the value if null, I don’t see an issue with setting it universally.

 

Also, I’ve never seen this before, and I’m psyched to learn it exists. PXDatabase.Update sucks.

Update<FeaturesSet>.
    Set<FeaturesSetExt.usrMyFeature.EqualTo<False>>.
    Where<FeaturesSetExt.usrMyFeature.IsNull>.
    Update(featuresMaint);

Edit: I see now that this is not a replacement for PXDatabase.Update, but it’s still cool.


mawe4585
Freshman II
  • Author
  • Freshman II
  • 7 replies
  • May 15, 2024
darylbowman wrote:

Is there a reason you would only want to affect the current tenant? As long as you’re only setting the value if null, I don’t see an issue with setting it universally.

 

An SQL script is executed once, when you publish the customization, after that never again.

So if you add another tenant, it will have NULL again and the SQL doesn’t execute anymore.

The CustomizationPlugin is executed everytime the Customization gets published.

Thus, it will be executed for every tenant as well.

Also, when you execute the SQL for all, it may also effect Snapshots.


darylbowman
Captain II
Forum|alt.badge.img+13
mawe4585 wrote:

So if you add another tenant, it will have NULL again and the SQL doesn’t execute anymore.

True, but if you’re defaulting the value to false, this will take care of that, since the only reason the default isn’t working now is because the record already exists and isn’t calling the defaulting event. A new record would call the defaulting event, thereby setting the value to false.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings