How to get SObject by a String(Full Object API Name)
Lately at my work, we had an issue where we were using Type.forName to get the ObjectType and that was returning the object which was inside of the managed package, and we want to return an object that was outside of the name package, even though we were calling the object without a namespace.
These code below shows how long it takes to get a normal standard object using 3 ways to get the object.
These codes were running in the Apex Developer Console » Execute Anonymous Windows.
Each group of code was executed by itself, which means, I didn’t execute all the groups at once, I executed one group once, then again 3 times.
My conclusion:
I will be trying out the JSON one, the JSON.Serialize supports my use case and the performance is better than Schema.getGlobalDescribe();.
What I have tried with Type.forName
I tried to use the Namespace, passing Type.forName(‘’, ‘MyCustomObject__c’);, it didn’t work, it still returns my namespace object which also is called ‘MyCustomObject__c’, but it has a namespace.
I tried to use a namespace called ‘Schema’, like Type.forName(‘Schema’, ‘MyCustomObject__c’), it didn’t work either, it got my object with namespace.