If I create a dynamic SOQL query (via strings, to be used with Database.query) in an unmanaged code environment, and then the code is converted to a managed environment, are namespace considerations taken into account? Or do I need to ensure I've prefixed my managed object names with the namespace of the managed package while assembling the dynamic SOQL query.
If you create dynamic SOQL queries and then later go to package the code in a managed package, you don't need to consider the prefix in your queries, the platform will magically know that the objects/fields are part of your package (assuming they were) and do the magic for you.
For example, if you had this SOQL in a class:
List<SObject> listOfThings = Database.query('Select MyField__c From Things__c');
And then eventually converted the package to a managed package with namespace prefix MyPKG you would not need to code it as:
List<SObject> listOfThings = Database.query('Select MyPKG__MyField__c From MyPKG__Things__c');
Case 1). If the Organization contains Namespace. For ex. I have an Object (Object_Analysis__c) in my Org with namespace(Naveen_catch) , Querying with or without namespace makes no difference within my org. i.e. List listOfThings = [Select Name From Object_Analysis__c]; or List listOfThings = [Select Name From Naveen_catch__Object_Analysis__c];
or we can also write dynamic queries like this
List<SObject> listOfThings = Database.query('Select Name From Object_Analysis__c'); List<SObject> listOfThings = Database.query('Select Name From Naveen_catch__Object_Analysis__c'); .
All above queries will work successful.
PS : Someone asked this query in twitter , but I forgot who asked it. :)
Regards, Niranjan, Senior Program Architect, AutoRABIT.