3

The FMA allows named booleans, integers and dates to be set by the creator of a managed package so they can turn package features on and off per customer.

The boolean option is pretty obvious: every user in the customers org gets the feature or doesn't get the feature. The managed package code just checks the flag.

But what is an effective way to handle the case where you want only up to say 50 of the users to be able to use a feature? You can set that limit via the FMA named integer option, and get the value in the managed package code. But you also need to be able to keep count of how many users in the customer org actually are using the feature so as to be able to block usage 51.

What is the best mechanism to persist this count? A Custom Metadata Type? A Custom Setting? And how to handle User deactivation, where you want to decrement the feature use count; does that require a trigger on the User object?

Duh, the following doesn't work because the field can't be protected from modification by the customer.

Or add a custom checkbox field on the User object and an on the fly count:

// Adds a query so can't precede a callout
public static Boolean checkFeatureAbcAvailable() {

    Boolean assigned = [
        select FeatureAbcAssigned__c
        from User
        where Id = :UserInfo.getUserId()
    ].FeatureAbcAssigned__c;

    if (assigned) return true;
    else return assignFeatureAbc();
}

// Adds an additional query and a DML
private static Boolean assignFeatureAbc() {

    Integer available = FeatureManagement.checkPackageIntegerValue('Feature ABC');

    Integer used = [
        select count()
        from User
        where IsActive = true
        and FeatureAbcAssigned__c = true
    ];

    if (used < available) {
        update new User(
            Id = UserInfo.getUserId(),
            FeatureAbcAssigned__c = true
        );
        return true;
    } else {
        return false;
    }
}

PS Just found and upvoted the similar - but not identical - question here Use FMA Features for per-user licensing. I've posted what I presently plan to do there but would be delighted if there was a better way to go.

  • If youre trying to block the 51st user of your feature (ie a page for example) perhaps as you suggested use a hierarchy custom setting. For the page/feat example one could leverage page load action to populate the custom setting or throw an error if the count of those setting records exceed the FMA Named Integer. I am considering this design in my appexchange package but i’m not sure yet if its best practice. – Peter Noges Apr 25 at 8:52
  • Thanks Peter. Yeah, kinda two parts: an enforcement mechanism and then where to call it from. – Keith C Apr 25 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.