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.