Ad Code

How to check available license in salesforce lightning

Automate available license info. alert in salesforce

Are you responsible for managing licenses in your Salesforce org? Do you need to check how many licenses are available at any given time? In this post, we'll show you how to easily check the available license count in the Salesforce Lightning Experience.

Retrieve total and used licenses in Salesforce org using the following SOQL query.

SELECT Id, LicenseDefinitionKey, TotalLicenses, Status, UsedLicenses, UsedLicensesLastUpdated, Name, MasterLabel, MonthlyLoginsUsed, MonthlyLoginsEntitlement, CreatedDate, LastModifiedDate, SystemModstamp FROM UserLicense

SOQL to retrieve available license in salesforce

TotalLicenses and UsedLicenses give an idea about how many licenses are left for assignment.

In addition to the above query, we can also get the license information from the setup.

Log in to Salesforce > Go to setup > Search for Company in the Quick Find search box (1) > Navigate to Company Information (2) > User Licenses

Company information in the salesforce org

It is crucial to regularly monitor the available licenses to ensure the organization can acquire new licenses before reaching the limit.

To streamline the process, consider implementing automation to send regular weekly or monthly reports on available licenses, eliminating the need for admins to log in regularly and manually check the details.

To set up an automation that sends license details to an admin email addresses at regular intervals, follow these three steps:

Step 1: Create Apex Handler Class (Name: LicenseInfoNotificationHandler)

public
class LicenseInfoNotificationHandler {
    public static void SendLicenseInfoEmail(){
        list<UserLicense> licenseList = [SELECT Id, LicenseDefinitionKey, TotalLicenses, Status, UsedLicenses, UsedLicensesLastUpdated, Name, MasterLabel, MonthlyLoginsUsed, MonthlyLoginsEntitlement, CreatedDate, LastModifiedDate, SystemModstamp FROM UserLicense];

        if(!licenseList.isEmpty()){
            //get Email Ids from custom metadata
            list<string> emailIds = new list<string>();
            list<User_License_Notification_Email_Id__mdt> userLicNotificationList = [SELECT Email_Id__c from User_License_Notification_Email_Id__mdt];
            for(User_License_Notification_Email_Id__mdt ul:userLicNotificationList){
                emailIds.add(ul.Email_Id__c);
            }
           
            //send an email
            if(!emailIds.isEmpty()){
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setToAddresses(emailIds);          
                Organization orgDetails = [SELECT Id, Name FROM Organization WHERE Id = :UserInfo.getOrganizationId() limit 1];
                String subject = 'Salesforce org: ' + orgDetails.Name + ' - User licenses information';
                email.setSubject(subject);
                email.setHtmlBody(CreateHTMLBody(licenseList));
                try {
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
                } catch (Exception e) {
                    System.debug('Error sending email: ' + e.getMessage());
                }  
            }  
        }
    }
   
    public static string CreateHTMLBody(list<UserLicense> licenseList){
        //open table..
        String htmlBody = '<table border="1" style="border-collapse: collapse"><tr>'+
            '<th>License Name</th><th>Total Licenses</th><th>Used Licenses</th></tr>';
        for(UserLicense ul : licenseList){
            htmlBody += '<tr><td>' + ul.Name + '</td><td>' + ul.TotalLicenses + '</td><td>' +
                ul.UsedLicenses + '</td></tr>';        
        }
        //close table...
        htmlBody += '</table>';
        return htmlBody;
    }
}


Step 2: Create Schedulable Apex Class (Name: LicenseInfoNotificationSchedule)

public class LicenseInfoNotificationSchedule implements Schedulable {
    public void execute (SchedulableContext ctx){
        LicenseInfoNotificationHandler.SendLicenseInfoEmail();
    }
}
 
Step 3: Schedule the class

Log in to Salesforce > Go to setup > Search with the apex in the quick find search box (1) > Select Apex Classes (2) and click Schedule Apex button (3)

Apex classes view in salesforce setup

After filling out all the necessary details, click the Save button.

Schedule apex from salesforce setup

Check the status of the job in the All-scheduled jobs or Apex jobs screen

Scheduled jobs view in the salesforce setup


Apex jobs view in the salesforce setup

You can also schedule this class from the Anonymous window of a developer console. Example code below.

LicenseInfoNotificationSchedule m = new LicenseInfoNotificationSchedule();
String sch = '0 0 12 ? * FRI *';
String jobID = System.schedule('License notification Job', sch, m);

Result

The license information received in the email looks as below

License information alert email


Considerations
  • Using a custom metadata type to store email addresses can eliminate the need to modify code when sending reports to new email addresses. By updating the email address within the custom metadata type, your code can retrieve it automatically. Find step-by-step instructions to create a custom metadata type in Salesforce Lightning.
  • If you prefer not to see all the user licenses in the report, you can apply a filter to the userlicense object query. This will allow you to limit the results to only the licenses you want to see, simplifying the report and reducing unnecessary data.
  • Check your organization's policies before sending the license information to external email addresses

Wrap up

In conclusion, being aware of your organization's available licenses in Salesforce Lightning is vital for effective resource allocation. By utilizing the steps outlined in this article, you can efficiently check the number of licenses at your disposal.

If you have any questions or comments, please leave them in the comments section below.

Until then…

Post a Comment

0 Comments