+ Start a Discussion

Identify if Managed or unmanaged package in an Apex Trigger

In an ApexTrigger is there a way to identify if the Trigger is being called from Managed or unmanaged Package ?

Same Trigger is called from a  Managed or Unmanaged package. Due to validation rules it fails when being called from Managed package.

I would like to check the package before calling the validation rules on the Trigger.




There isn't a function call for "running in managed mode" that you can use directly. But, you can use:


Boolean isManaged, isUserLicensed;
try {
  isUserLicensed = System.isCurrentUserLicensed('mynamespace');
  isManaged = true;
} catch(exception e) {
  isManaged = false;

If isManaged is true, you are executing from the managed package you named (because, if you were running from an unmanaged code where this namespace did not exist, and exception would be thrown, the line isManaged = true would not execute, but isManaged = false would). If you are in the managed package, you can then tell if the user has a license to use the app (isUserLicensed). However, using this technique, you would not be able to determine if both packages were installed (because it would report as being managed).