---
schema_version: "1"
id: 6ab52e1aa430a9b55bd03c437fb557ec9574bcbe89e0e367c0954a8588b7784e
title: PackageLicense
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/object_reference/sforce_api_objects_packagelicense.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_packagelicense.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 701345f8fb060a8f064a6238d5d76e5deb6796ec56fde56f1cdade5c706188e9
description: Represents a license for an installed managed package. This object is available in API version 31.0 and later.
---
# PackageLicense

Represents a license for an installed managed package. This object is available in API version 31.0 and later.

## Supported Calls

describeSObjects(), query(), retrieve()

## Special Access Rules

Customer Portal users can't access this object.

## Fields

| Field Name | Details |
| --- | --- |
| AllowedLicenses | 
Type

int

Properties

Filter, Group, Sort

Description

The number of users allowed to use the package.



 |
| ExpirationDate | 

Type

dateTime

Properties

Filter, Nillable, Sort

Description

The date and time when the package license expires.



 |
| IsAvailableForIntegrations | 

Type

boolean

Properties

Defaulted on create, Filter, Group, Sort

Description

Reserved for future use.



 |
| IsProvisioned | 

Type

boolean

Properties

Defaulted on create, Filter, Group, Sort

Description

Reserved for internal use.



 |
| NamespacePrefix | 

Type

string

Properties

Filter, Group, Sort

Description

The namespace prefix associated with the package.



 |
| Status | 

Type

picklist

Properties

Filter, Group, Restricted picklist, Sort

Description

The status of the license. Possible values are: Active, Expired, Free, and Trial.



 |
| UsedLicenses | 

Type

int

Properties

Filter, Group, Sort

Description

The number of users who have a license to the package.



 |

## Usage

Use this object to determine the number of licenses allowed and in use for a managed package installed in your organization.

The following example demonstrates the use of the API to manage licenses for a package. The example defines an Apex class that does the following.

- Retrieves the PackageLicense record for the specified package (identified by its namespace prefix).
- Defines a function that returns a list of all users with the specified profile.
- Creates a UserPackageLicense record for each user with that profile, which has the effect of assigning a license for the package to all users with that profile.
- Returns an error message if the number of users exceeds the number of available licenses.

public class AssignPackageLicense {
     
    static String PACKAGE\_NAMESPACE\_PREFIX = 'acme\_101';
    static String PROFILE\_ID = '00exx000000jz1SAAQ';
    public static String exceptionText {get; set;}
        
    public AssignPackageLicense() {
         exceptionText = 'Initialized';
    }

 		static List<User> getUsersWithProfile(){
      String userQuery = 'SELECT Id FROM User WHERE ProfileId = :PROFILE\_ID';
      List<User> matchingUsers = new List<User>();
      matchingUsers = \[SELECT Id FROM User WHERE ProfileId = :PROFILE\_ID\];
      return matchingUsers;
    }

    public static void assignLicenseByProfile() {
        //find the PackageLicense Id
        PackageLicense pl = \[SELECT Id, NamespacePrefix, AllowedLicenses, UsedLicenses, 
        					 ExpirationDate,Status FROM PackageLicense WHERE 
        					 NamespacePrefix = :PACKAGE\_NAMESPACE\_PREFIX\];
        System.assert(pl != null, 'PackageLicense cannot be null.');
        List<User> usersToAssignLicenses = getUsersWithProfile();
        List<UserPackageLicense> firstUPLs = new List<UserPackageLicense>();
        
        //create a new UserPackageLicense record for each user with the specified profile
        for (Integer i = 0; i< usersToAssignLicenses.size(); i++){
              UserPackageLicense upl = new UserPackageLicense();
              upl.PackageLicenseId = pl.Id;
              upl.UserId = usersToAssignLicenses\[i\].Id;
              firstUPLs.add(upl);
        }
        
        try {
          //bulk insert
          insert(firstUPLs);
          } catch(DmlException e) {
             for (Integer i = 0; i < e.getNumDml(); i++) {
             // process exception here 
               System.debug(e.getDmlMessage(i)); 
               String status = e.getDmlStatusCode(i);
               System.debug(status + ' ' + e.getDmlMessage(i));
               if(status.equals('LICENSE\_LIMIT\_EXCEEDED')){
                exceptionText = 'You tried to assign more licenses than available. ' 
                +' You tried to create '\+ firstUPLs.size()+' licenses but only have '
                + (pl.AllowedLicenses - pl.UsedLicenses) + ' licenses free.';
                System.debug(exceptionText);
               }
             }
         }
    }    
 }