Show warning managed package expired/trial version

Telegram logo Join our Telegram Channel

Hello, Cloudiers!! 

Unlike Visualforce pages and components, Aura Components and Lightning web components do not hide the contents if the user does not have a managed license assigned or the package is expired.

Do you want to show a warning message to users when the package is being expired or in the trial version. Or maybe you want to restrict the access of Aura components and Lightning web components when the logged-in user does not have a license assigned. 

In this post, I will show you how you can check the managed package license status using the apex code. 

I will demonstrate the below scenarios in this post.
  1. To display the warning message if the license of the managed package is being expired or in a trial version.
  2. Hide the content inside the Aura Component/Lightning web component if the user doesn't have a managed package license assigned to it.
Ok, so let's power through it!


Query(SOQL) the status of the managed package.

You can query all details about the installed managed package, all details are stored in the object "PackageLicense". 
SELECT Status, ExpirationDate FROM PackageLicense WHERE NamespacePrefix = 'Package_Namespace'

The above query will give you the status and expiration DateTime for the managed package. Status can have these values Active, Expired, Free, and Trial.

The PackageLicense also stores the details about licenses in the fields AllowedLicenses and UsedLicenses.

To use this in the Lightning web component, you need to query this using wired apex. I have demonstrated that in another post "Hide contents of Lwc if managed package expired".


Check if user has managed package license

Information about assigned licenses to the user is stored in the object "UserPackageLicense". UserPackageLicense is a junction object between the User and PackageLicense object discussed above section.

So we can get the details using a relationship query like below.
SELECT 
    UserId,PackageLicense.Status 
FROM UserPackageLicense 
WHERE PackageLicense.NamespacePrefix = 'Package_Namespace' AND UserId = '<user-id>'


Query all users who have managed license

Additionally, you can query all of the users who have been assigned the license to your managed package. Also, you can get their profile and other details. This can be very handy when you want to show the list of the users inside the admin panel of your app.
SELECT UserId, PackageLicense.Status FROM UserPackageLicense WHERE PackageLicense.NamespacePrefix = 'Package_Namespace'


Query all of the installed packages

Do you want to check which packages are installed in your org? Well, that is not supported as of now with apex. You need to use metadata APIs for that. But if you just want to get the list with namespace names and status, use the below query.
SELECT 
    Status, ExpirationDate,NamespacePrefix 
FROM PackageLicense


Conclusion

Ok, that's it this is how you can track the license status of the user and inside lightning components. I also shared some other tips like query all users with license, check the status of all packages using SOQL.

I hope this has helped you!

Do you think it is helpful? Do you want me to cover some topics like this? Let me know in the comments or you can contact me directly on twitter, or contact page on this blog.

Peace!
3 comments:
  1. Great post.

    Can we do it for Aura Components?
    When a user doesn't have the access of the package then he doesn't have the access of the Apex class as well. So, the Aura component doesn't able to call the Apex controller because the Apex class is inaccessible.

    Do we have another way without querying or querying inside the JS controller of aura component?

    ReplyDelete
    Replies
    1. Hello Dear Unknown, This will be work fine when your package is in trial version. But for the expired and license not provided case, I think you can handle that in the error handler of aura/lwc component. Aura fires some error when the apex class is not accessible due to licensing, but this I haven't tried yet. If you can give it a try and find something please do let me know.

      Delete
    2. Actually the error is fired when you try to call the AuraEnabled apex from components.

      Delete

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com