[SalesForce] Get Namespace Prefix from calling class

I've got an issue with some managed package test classes (specifically CPQ test classes), where I'm running into SoQL limits when certain test classes execute. What I'd like to do is disable certain triggers when the test class originates from the managed package.

Is there a way to access/identify the namespace prefix of the class that initiated the process programatically in order to disable triggers from firing?

Best Answer

One of the approaches that I could think of is to utilize sort of a "marker interface" which determines if the trigger needs to be executed or not based on a flag. In any case, you need to handle this within your trigger logic even if you are able to identify the context of the running trigger. It's more a design choice how you will want to achieve this, but something as below will work in such cases.

Let's say I define a simple class as:

public class IMarkerInterface {
    public static Boolean RUN_TRIGGER = false;
}

And in my trigger, I would use this class to branch the execution logic something as:

if(IMarkerInterface.RUN_TRIGGER == false) {
    // skip
} else {
    // execute the logic
}

Now for all my test class written, I would have set this flag to true IMarkerInterface.RUN_TRIGGER = true; in my test methods, so that all my local test classes will always execute this logic.

The ones from managed package will always skip the trigger logic, because they would not have had set this value.

Related Topic