1

Below is an example

trigger **TestABCTrigger** from Account(after insert,after update) {

    string triggerName = **TestABCTrigger**;
}

I want the name so that I can create a dynamic version of the trigger handled by custom object. Is there any way to get the Trigger name and store that in the string?

2 Answers 2

2

There's probably a better way to do this but here's an approach relying on an artificially-generated exception's getStackTraceString().

Write a Util class method as follows:

//  ------------------------------------------------------------
//  getExecutableName : Return name of calling trigger, class, or class.innerClass  
//  ------------------------------------------------------------
public static String getExecutableName() {
    String res;
    //  Stacktrace will look like
    //  Class.someClassName.someMethodName: line/column
    //  Class.someClassName.someInnerClass.someMethodName: line/column
    //  ....    
    //  Trigger.someTriggername:  
    try {Integer i = 10 / 0;}
    catch (Exception e) {
        String[] lines = e.getStackTraceString().split('\n');
        for (Integer i = lines.size()-1; i >= 0; i--) {
            if (lines[i].startsWith('AnonymousBlock:')) continue;       // in case invoked from anon apex
            if (lines[i].startsWith('Trigger.'))
                return lines[i].subStringBetween('Trigger.',':');
            else {      // could be Class.class.method or Class.outerclass.innerclass.method
                String candidate = lines[i].substringBetween('Class.',':');
                return candidate.substring(0,candidate.lastIndexOf('.')); 
            }   
        }
    } 
    return res;
} 

In your trigger, do this:

trigger **TestABCTrigger** from Account(after insert,after update) {

    string triggerName = Util.getExecutableName();
}

As exceptions are somewhat expensive, I wouldn't use this in some tight loop.

I prototyped the above and it works in the trigger, outer class, inner class, and execute anonymous use cases. There may be other use cases I didn't think of so your mileage may vary.

0

There is no way to do that in Apex as far as I'm aware. You can just use the variable triggerName and keep it the same as the name of the Trigger though. Then you can pass triggerName to your custom object?

6
  • I have a line like this in every class and trigger: public static final String CLASSNAME = 'the name of the class';
    – user735
    Feb 3, 2015 at 17:23
  • And what's wrong with that solution? Sorry if I don't exactly understand your requirement :-) Feb 3, 2015 at 17:36
  • Nothing wrong with it at all! (I am agreeing with you.)
    – user735
    Feb 3, 2015 at 18:50
  • Oh, my mistake! I confused you with the person who asked the question :-) Feb 3, 2015 at 18:52
  • I think you could throw an exception and get it from getStacktraceString(). The catch would not abort anything
    – cropredy
    Feb 3, 2015 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.