We have developed a managed package application which is currently privately listed in AppExchange and is soon to me made public. Our trial users have installed our application in their production orgs. Now after their trial install expires, the scheduled jobs created by the application throw System.LicenseException.

Does anyone out here has the information about how to handle this exception and possibly remove the scheduled job using code when trial has expired?

  • Does the solution shown here help? boards.developerforce.com/t5/Apex-Code-Development/… Might be difficult cause of the nature of the managed package install. Can you not uninstall the trial?
    – pbattisson
    Oct 22, 2012 at 8:59
  • Thanks for the reply. No, that doesn't help. I am unable to uninstall it when I delete jobs. But that's not the issue. I want to handle this exception and then possibly delete the job or email the admin to uninstall or delete the job. Oct 22, 2012 at 13:01
  • Mustafa, did either of the answers posted help solve your issue? If so please mark one of the answers as accepted so that others who have the same problem will know how to avoid it!
    – zachelrath
    Nov 1, 2012 at 11:33

2 Answers 2


The best way to solve this depends on the nature / importance of the scheduled jobs being created by your application, and on how you're dynamically scheduling these jobs.

The best solution, if possible, is to have a non-packaged version of the Schedulable class, in addition to the Scheduled class, that you provide along with your application. Obviously this is a lot easier with Trialforce orgs, if this is an option, but depending on how annoying the LicenseException error is, it might be worth it. What you can do is to check for a local version of your Schedulable class, with the same name, and try to use that if it exists, otherwise default to the packaged version:

// First, check to see if the running user is licensed to the acme package.
// If not, do nothing else
if (!UserInfo.isCurrentUserLicensed('acme')) {
   // OPTIONAL - may be helpful in your scenario
   // System.abortJob(jobId)

   // Do nothing more

// Get a schedulable class to run
System.Type t;
Schedulable c;
try {

    // First, check the local namespace
    // In Trialforce orgs, this class will exist
    t = System.Type.forName(null,'MySchedulableClass');

    // Second, check for an instance of this class
    // in our namespace (e.g. 'acme')
    if (t == null || String.valueOf(t).contains('null')) {
        t = System.Type.forName('acme','MySchedulableClass');

    // If we found an available Schedulable class, use it!  
    if (t != null && !String.valueOf(t).contains('null')) {
        c = (Schedulable) System.JSON.deserialize('{}',t);
} catch (Exception e) {}

// If we found a Schedulable class, schedule it to run
if (c != null) {

This is probably the best solution if you are only scheduling your job once, or if you do not have a chance to dynamically reschedule it later.

The really difficult thing about all of this is that you'll get the LicenseException from ANY code you have pre-packaged in your managed app, so even scheduling a job to run 31 days after the install to check if the user is still licensed will not help, because that job will be run using code in the package, which leaves you in the same place you started.


If what you are doing is trying to detect the exception before it exists you could use the static method


This would by inference allow you to determine whether the package has expired. Be careful though, it could still return false when the package is active but the running user is not a licensed user.


As pointed out by @zachelrath, This solution is only useful if your scheduled apex class is outside the managed package. The license limit exception is thrown before the scheduled job is executed.

  • Daniel, in this scenario, the initial entry point into Mustafa's code is the Apex Scheduler's invocation of his scheduled class. That is, there is NO opportunity for the UserInfo.isCurrentUserLicensed() method to be included in Mustafa's managed package's code such that would be invoked prior to the Apex runtime's invocation of the packaged scheduled class, at which point Mustafa is getting the LicenseException. The isCurrentUserLicensed() method could, however, be helpful if used in an unpackaged scheduled class, as I have suggested.
    – zachelrath
    Oct 23, 2012 at 16:05

You must log in to answer this question.

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