0

Is there a more performant way to dynamically detect if Content Notes is enabled?

I see that getGlobalDescribe can be used to check the presence ContentNote, if it present then is enabled. Usually, the time is around 400-500ms on the first call and very little in subsequent calls - around 40ms. So, I see caching reducing the CPU Time consumption for subsequent calls but if the initial call could be reduced, that would be even better.

Getting rid of the describe call is a possibility if is reliable and more performant.

e.g.:

Map<String, SobjectType> gd = Schema.getGlobalDescribe(); Boolean isContentNotesEnabled = gd.get('ContentNote') != null; 

2 Answers 2

1

I'd recommend using Schema.ContentNote instead of just ContentNote. Note that a developer could still use the class named Schema, so you'd also make sure there's no Schema class:

if(Type.forName('Schema') == null) { if(Type.forName('Schema.ContentNote') != null) { // We have ContentNote! } } else { if(Schema.getGlobalDescribe().get('ContentNote') != null) { // We have ContentNote! } } 

Note that if someone defines a Schema class, we have to fallback to the slower (500ms) method, as this is always safe, but can use the much faster (<1ms, I think) method otherwise.

2
  • All of this trouble because Salesforce doesn't have a way that performant and native to inform code if Content Notes are enabled. Commented May 14, 2020 at 17:16
  • Was just revisiting this, do you think is worth doing if(Type.forName('Schema', 'ContentNote') != null) { to avoid the both initial if statements? Commented Jun 11, 2020 at 18:15
0

I don't know if there's a more direct way, but there's a more efficient way to do what your check does:

Type contentNoteType = Type.forName('ContentNote'); System.debug(contentNoteType != null && contentNoteType.newInstance() instanceof SObject); 

This saves you getting the global schema.

3
  • That is a very interesting approach, could not it stop to work if there is a class named ContentNote? Commented May 14, 2020 at 13:49
  • Only if the class called ContentNote didn't have a default constructor. If it does, that code would still get the right answer because it's checking the type of the new instance. If it doesn't have a default constructor, an exception is thrown (which I guess you could catch) Commented May 14, 2020 at 14:46
  • Got you, the problem is the collision, because if it happens then it is back to square one to check if ContentNote is a valid sObject (even catching the exception). Commented May 14, 2020 at 15:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.