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.