[SalesForce] Dynamic SOQL Queries and Managed Namespaces

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.

Best Answer

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');