In an app that I'm creating, there are some places where I need to create objects based on their string name, and need to take the apps namespace prefix into account dynamically.
I've created this function that will grab the ns based on one of the custom object names:
public static String getNamespacePrefix() {
String namespace = '';
try {
ApexClass ac = [SELECT NameSpacePrefix FROM ApexClass WHERE Name = 'CUSTOM_CLASS_NAME'];
if(null != ac.NameSpacePrefix) {
namespace = ac.NameSpacePrefix + '__';
}
} catch (QueryException ex) {
// do nothing
}
return namespace;
}
There are a handful of places that call this function which is causing me to run into SQL query limits.
Is there a better way to do this? Or a way to make the namespace somehow globally available one time?