Timeline for Apex: Getting namespace prefix
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 21, 2021 at 13:16 | comment | added | Andrii Muzychuk | Doesn't work in client's org with installed package, the code gives Error: Invalid type: [classFromMyPackage] | |
Sep 12, 2018 at 5:45 | comment | added | Naval Sharma | @navD87 What if there are 2 managed packages and they are having the same class name. For example Utils.class. Will it work in that case too? | |
Jun 12, 2015 at 12:26 | vote | accept | powlo | ||
Jun 8, 2015 at 14:00 | comment | added | Mr. DOS | That's right. The “calculation” will be performed the first time it is requested within the scope of a page load. But in this case, I don't think it's a huge overhead. If it were really bad, you could store the result of the calculation in a hierarchical custom setting, then construct a two-level caching solution which retrieves the value of the custom setting into a static field. (I've never run into a situation which warranted that, but YMMV.) | |
Jun 8, 2015 at 11:29 | comment | added | user39399 | @Mr.DOS thanks for the sample. This cache is only alive until the page is closed and the apex transaction ends, isn't it? I guess it could be more useful in java or c#. | |
Jun 5, 2015 at 17:44 | comment | added | Mr. DOS | @bananka I've written an example of this pattern. Hopefully this will clear up what I'm suggesting. | |
Jun 5, 2015 at 15:45 | comment | added | user39399 | @Mr.DOS but wouldn't null be returned on the next call in any case?.. | |
Jun 5, 2015 at 15:08 | history | edited | navD87 | CC BY-SA 3.0 |
added 33 characters in body
|
Jun 5, 2015 at 14:55 | comment | added | Mr. DOS |
@bananka If I'm going to put the method in a utility class anyway, I'd add a static field to that class (private static String namespacePrefix = null; ). At the top of the getNamespacePrefix() method, I'd check to see if that field was non-null, and if so, I'd return it. Otherwise, I'd go through the process of determining the namespace prefix, but right before return ing, I'd set the value of the static field.
|
|
Jun 5, 2015 at 14:41 | comment | added | powlo | What happens if there are two classes called "My.class"? It seems like there's no way for an apex file to be able to access information about itself. | |
Jun 5, 2015 at 10:24 | comment | added | user39399 | @Mr.DOS how would you cache it? | |
Jun 4, 2015 at 17:12 | comment | added | navD87 | Yes, I agree. This is just an example. If you are going to use this, do as Mr.DOS has suggested. | |
Jun 4, 2015 at 17:09 | comment | added | Mr. DOS | If you're going to call this method frequently, it might be worth making it static, putting it in a utility class, and having it cache the result for subsequent calls. | |
Jun 4, 2015 at 17:05 | history | answered | navD87 | CC BY-SA 3.0 |