{"id":"sforce_api_objects_packagelicense","title":"PackageLicense","content":"<a name=\"sforce_api_objects_packagelicense\"><!-- --></a><h1 class=\"helpHead1\">PackageLicense</h1><div class=\"body\">\n<div class=\"shortdesc\">\n<span class=\"ph\" id=\"summary\"><a name=\"summary\"><!-- --></a><span class=\"ph\" id=\"description\"><a name=\"description\"><!-- --></a>Represents a license for an installed managed\n        package.</span>\n    </span>This object is available in API version 31.0 and later.</div>\n\n    <div class=\"section\" id=\"section1\">\n<a name=\"section1\"><!-- --></a><h2 class=\"helpHead2\">Supported Calls</h2>\n\n      \n      <p class=\"p\"><samp class=\"codeph nolang\">describeSObjects()</samp>, <samp class=\"codeph nolang\">query()</samp>, <samp class=\"codeph nolang\">retrieve()</samp></p>\n\n    </div>\n\n    <div class=\"section\">\n<h2 class=\"helpHead2\">Special Access Rules</h2>\n\n      \n      <p class=\"p\"><span class=\"ph\" id=\"CustPortalNoAccess\"><a name=\"CustPortalNoAccess\"><!-- --></a>Customer Portal users can't access this object.</span></p>\n\n    </div>\n\n    <div class=\"section\" id=\"section2\">\n<a name=\"section2\"><!-- --></a><h2 class=\"helpHead2\">Fields</h2>\n\n      \n      \n<div class=\"data colSort\"><table class=\"featureTable sort_table\" summary=\"\">\n          \n          \n          <thead class=\"thead sorted\" align=\"left\">\n            <tr>\n              <th class=\"featureTableHeader  \" id=\"d1448634e86\">Field Name</th>\n\n              <th class=\"featureTableHeader  \" id=\"d1448634e89\">Details</th>\n\n            </tr>\n\n          </thead>\n\n          <tbody class=\"tbody\">\n            <tr id=\"AuthCode\">\n              <td class=\"entry\" headers=\"d1448634e86\" data-title=\"Field Name\"><span class=\"keyword parmname\">AllowedLicenses</span></td>\n\n              <td class=\"entry\" headers=\"d1448634e89\" data-title=\"Details\">\n                <dl class=\"dl detailList\">\n                  \n                    <dt class=\"dt dlterm\">Type</dt>\n\n                    <dd class=\"dd\">int</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Properties</dt>\n\n                    <dd class=\"dd\">Filter, Group, Sort</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Description</dt>\n\n                    <dd class=\"dd\">The number of users allowed to use the package.</dd>\n\n                  \n                </dl>\n\n              </td>\n\n            </tr>\n\n            <tr id=\"ConnectedAppCallbackUrl\">\n              <td class=\"entry\" headers=\"d1448634e86\" data-title=\"Field Name\"><span class=\"keyword parmname\">ExpirationDate</span></td>\n\n              <td class=\"entry\" headers=\"d1448634e89\" data-title=\"Details\">\n                <dl class=\"dl detailList\">\n                  \n                    <dt class=\"dt dlterm\">Type</dt>\n\n                    <dd class=\"dd\">dateTime</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Properties</dt>\n\n                    <dd class=\"dd\">Filter, Nillable, Sort</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Description</dt>\n\n                    <dd class=\"dd\">The date and time when the package license expires.</dd>\n\n                  \n                </dl>\n\n              </td>\n\n            </tr>\n\n            <tr>\n              <td class=\"entry\" headers=\"d1448634e86\" data-title=\"Field Name\"><span class=\"keyword parmname\">IsAvailableForIntegrations</span></td>\n\n              <td class=\"entry\" headers=\"d1448634e89\" data-title=\"Details\">\n                <dl class=\"dl detailList\">\n                  \n                    <dt class=\"dt dlterm\">Type</dt>\n\n                    <dd class=\"dd\">boolean</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Properties</dt>\n\n                    <dd class=\"dd\">Defaulted on create, Filter, Group, Sort</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Description</dt>\n\n                    <dd class=\"dd\">Reserved for future use.</dd>\n\n                  \n                </dl>\n\n              </td>\n\n            </tr>\n\n            <tr>\n              <td class=\"entry\" headers=\"d1448634e86\" data-title=\"Field Name\"><span class=\"keyword parmname\">IsProvisioned</span></td>\n\n              <td class=\"entry\" headers=\"d1448634e89\" data-title=\"Details\">\n                <dl class=\"dl detailList\">\n                  \n                    <dt class=\"dt dlterm\">Type</dt>\n\n                    <dd class=\"dd\">boolean</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Properties</dt>\n\n                    <dd class=\"dd\">Defaulted on create, Filter, Group, Sort</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Description</dt>\n\n                    <dd class=\"dd\">Reserved for internal use.</dd>\n\n                  \n                </dl>\n\n              </td>\n\n            </tr>\n\n            <tr id=\"CreatedOrgInstance\">\n              <td class=\"entry\" headers=\"d1448634e86\" data-title=\"Field Name\"><span class=\"keyword parmname\">NamespacePrefix</span></td>\n\n              <td class=\"entry\" headers=\"d1448634e89\" data-title=\"Details\">\n                <dl class=\"dl detailList\">\n                  \n                    <dt class=\"dt dlterm\">Type</dt>\n\n                    <dd class=\"dd\">string</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Properties</dt>\n\n                    <dd class=\"dd\">Filter, Group, Sort</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Description</dt>\n\n                    <dd class=\"dd\">The namespace prefix associated with the package.</dd>\n\n                  \n                </dl>\n\n              </td>\n\n            </tr>\n\n            <tr>\n              <td class=\"entry\" headers=\"d1448634e86\" data-title=\"Field Name\"><span class=\"keyword parmname\">Status</span></td>\n\n              <td class=\"entry\" headers=\"d1448634e89\" data-title=\"Details\">\n                <dl class=\"dl detailList\">\n                  \n                    <dt class=\"dt dlterm\">Type</dt>\n\n                    <dd class=\"dd\">picklist</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Properties</dt>\n\n                    <dd class=\"dd\">Filter, Group, Restricted picklist, Sort</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Description</dt>\n\n                    <dd class=\"dd\">The status of the license. Possible values are: <span class=\"keyword parmname\">Active</span>,\n                        <span class=\"keyword parmname\">Expired</span>, <span class=\"keyword parmname\">Free</span>, and\n                        <span class=\"keyword parmname\">Trial</span>.</dd>\n\n                  \n                </dl>\n\n              </td>\n\n            </tr>\n\n            <tr>\n              <td class=\"entry\" headers=\"d1448634e86\" data-title=\"Field Name\"><span class=\"keyword parmname\">UsedLicenses</span></td>\n\n              <td class=\"entry\" headers=\"d1448634e89\" data-title=\"Details\">\n                <dl class=\"dl detailList\">\n                  \n                    <dt class=\"dt dlterm\">Type</dt>\n\n                    <dd class=\"dd\">int</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Properties</dt>\n\n                    <dd class=\"dd\">Filter, Group, Sort</dd>\n\n                  \n                  \n                    <dt class=\"dt dlterm\">Description</dt>\n\n                    <dd class=\"dd\">The number of users who have a license to the package.</dd>\n\n                  \n                </dl>\n\n              </td>\n\n            </tr>\n\n          </tbody>\n\n        </table></div>\n\n    </div>\n\n    <div class=\"section\" id=\"section3\">\n<a name=\"section3\"><!-- --></a><h2 class=\"helpHead2\">Usage</h2>\n\n      \n      <p class=\"p\" id=\"usage_p1\"><a name=\"usage_p1\"><!-- --></a>Use this object to determine the number of licenses allowed and in use for a\n        managed package installed in your organization.</p>\n\n      <div class=\"p\" id=\"usage_p2\">\n<a name=\"usage_p2\"><!-- --></a>The following example demonstrates the use of the API to manage licenses for\n        a package. The example defines an Apex class that does the following.<ul class=\"ul bulletList\">\n          <li class=\"li\">Retrieves the PackageLicense record for the specified package (identified by its\n            namespace prefix).</li>\n\n          <li class=\"li\">Defines a function that returns a list of all users with the specified profile.</li>\n\n          <li class=\"li\">Creates a UserPackageLicense record for each user with that profile, which has the\n            effect of assigning a license for the package to all users with that profile.</li>\n\n          <li class=\"li\">Returns an error message if the number of users exceeds the number of available\n            licenses.</li>\n\n        </ul>\n</div>\n\n      <div class=\"codeSection apex_code\"><pre class=\"codeblock brush:apex\" id=\"codeblock1\"><a name=\"codeblock1\"><!-- --></a><span class=\"keyword\">public</span> <span class=\"keyword\">class</span> AssignPackageLicense {\n     \n    <span class=\"keyword\">static</span> <span class=\"keyword\">String</span> PACKAGE_NAMESPACE_PREFIX = <span class=\"string\">'acme_101'</span>;\n    <span class=\"keyword\">static</span> <span class=\"keyword\">String</span> PROFILE_ID = <span class=\"string\">'00exx000000jz1SAAQ'</span>;\n    <span class=\"keyword\">public</span> <span class=\"keyword\">static</span> <span class=\"keyword\">String</span> exceptionText {get; set;}\n        \n    <span class=\"keyword\">public</span> AssignPackageLicense() {\n         exceptionText = <span class=\"string\">'Initialized'</span>;\n    }\n\n \t\t<span class=\"keyword\">static</span> List&lt;User&gt; getUsersWithProfile(){\n      <span class=\"keyword\">String</span> userQuery = <span class=\"string\">'SELECT Id FROM User WHERE ProfileId = :PROFILE_ID'</span>;\n      List&lt;User&gt; matchingUsers = <span class=\"keyword\">new</span> List&lt;User&gt;();\n      matchingUsers = [SELECT Id FROM User WHERE ProfileId = :PROFILE_ID];\n      <span class=\"statement\">return</span> matchingUsers;\n    }\n\n    <span class=\"keyword\">public</span> <span class=\"keyword\">static</span> <span class=\"keyword\">void</span> assignLicenseByProfile() {\n        <span class=\"onelineComment\">//find the PackageLicense Id</span>\n        PackageLicense pl = [SELECT Id, NamespacePrefix, AllowedLicenses, UsedLicenses, \n        \t\t\t\t\t ExpirationDate,Status FROM PackageLicense WHERE \n        \t\t\t\t\t NamespacePrefix = :PACKAGE_NAMESPACE_PREFIX];\n        System.assert(pl != <span class=\"keyword\">null</span>, <span class=\"string\">'PackageLicense cannot be null.'</span>);\n        List&lt;User&gt; usersToAssignLicenses = getUsersWithProfile();\n        List&lt;UserPackageLicense&gt; firstUPLs = <span class=\"keyword\">new</span> List&lt;UserPackageLicense&gt;();\n        \n        <span class=\"onelineComment\">//create a new UserPackageLicense record for each user with the specified profile</span>\n        <span class=\"statement\">for</span> (<span class=\"keyword\">Integer</span> i = 0; i&lt; usersToAssignLicenses.size(); i++){\n              UserPackageLicense upl = <span class=\"keyword\">new</span> UserPackageLicense();\n              upl.PackageLicenseId = pl.Id;\n              upl.UserId = usersToAssignLicenses[i].Id;\n              firstUPLs.add(upl);\n        }\n        \n        <span class=\"statement\">try</span> {\n          <span class=\"onelineComment\">//bulk insert</span>\n          <span class=\"statement\">insert</span>(firstUPLs);\n          } <span class=\"statement\">catch</span>(DmlException e) {\n             <span class=\"statement\">for</span> (<span class=\"keyword\">Integer</span> i = 0; i &lt; e.getNumDml(); i++) {\n             <span class=\"onelineComment\">// process exception here </span>\n               System.debug(e.getDmlMessage(i)); \n               <span class=\"keyword\">String</span> status = e.getDmlStatusCode(i);\n               System.debug(status + <span class=\"string\">' '</span> + e.getDmlMessage(i));\n               <span class=\"statement\">if</span>(status.equals(<span class=\"string\">'LICENSE_LIMIT_EXCEEDED'</span>)){\n                exceptionText = <span class=\"string\">'You tried to assign more licenses than available. '</span> \n                +<span class=\"string\">' You tried to create '</span>+ firstUPLs.size()+<span class=\"string\">' licenses but only have '</span>\n                + (pl.AllowedLicenses - pl.UsedLicenses) + <span class=\"string\">' licenses free.'</span>;\n                System.debug(exceptionText);\n               }\n             }\n         }\n    }    \n }\n</pre></div>\n\n    </div>\n\n  </div><div id=\"sfdc:seealso\" class=\"related-links\"></div>"}