3

I have an app on the App Exchange that is free. I'd like to develop some additional features that will cost money. Do I need to create two separate apps, or is it possible to create just one app?

I'm thinking there would need to be a way to restrict certain pages, features, and sObjects depending if the user has the appropriate license.

2 Answers 2

5

You'd want two packages, a base package and an extension. There's no way to license "part" of a package. The base package could detect the presence of the extension package, and call a method via an interface.

Base Package (namespace: basepack)


global class IPremiumVerifier {
    Boolean isLicensed();
}

public class CheckPremium {
    public static Boolean isLicensed() {
        try {
            return ((IPremiumVerifier)Type.forName('prempack','PremiumVerifier').newInstance()).isLicensed();
        } catch(Exception e) {
            return false;
        }
    }
}

Extension Package (namespace: prempack)

global class PremiumVerifier implements basepack.IPremiumVerifier {
    global Boolean isLicensed() {
        return UserInfo.isCurrentUserLicensed();
    }
}
3
  • My OOPS skills are not so great. So, I have a question: In the CheckPremium class, isLicensed method, we should cast the instance to PremiumVerifier or IPremiumVerifier?
    – javanoob
    Commented Nov 20, 2016 at 3:29
  • 1
    @javanoob yes, sorry, it would be IPermiumVerifier, just a typo. I've fixed it in the answer.
    – sfdcfox
    Commented Nov 20, 2016 at 3:38
  • @sfdcfox do I have to above approach even for this (salesforce.stackexchange.com/questions/222850/…) Commented Jun 26, 2018 at 13:57
4

If the features you want to restrict have Apex code e.g. Visualforce pages or @RestResource API's or SObject creates or updates or deletes (for which you have added triggers to enforce licensing), then you can use the UserInfo.isCurrentUserLicensed(namespace) to allow access for license users and block access for unlicensed users.

I caution against separating into two (or more) packages so you can use Salesforce's License Management App (LMA) unless you are sure you need to. It complicates the development process and because global classes and methods can't be changed once packaged makes it difficult to evolve your code.

Another alternative is to roll your own feature license mechanism within the one package. One approach is to issue encrypted feature keys that incorporate the org id so are tied to a particular customer's org. Another is to add checkbox custom fields to the License object in the LMA and for your app to call out to a site in your LMA org to check those flags.

2
  • That works great for code-only restrictions, but they did mention they wanted to strict access to objects based on paid licensing. I don't know of a way to do this in a single package.
    – sfdcfox
    Commented Nov 19, 2016 at 18:11
  • @sfdcfox Yep agreed.
    – Keith C
    Commented Nov 20, 2016 at 0:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .