6

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.

6

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');
  • 2
    But, on the flip side of this coin, if you have a field by a certain name, and it mirrors the same name as an unmanaged field in the organization, your code won't be able to query the unmanaged field by virtue of "shadowing." – sfdcfox Jul 2 '13 at 13:55
  • 1
    So, I think you're saying if you have managed MyPKG__MyField__c and unmanaged MyField__c on the same object, and you query it dynamically as in the managed package as MyField__c it won't be able to? – Phil Hawthorn Jul 2 '13 at 16:03
  • 2
    You would only be able to query MyPKG__MyField__c, but MyField__c would not be accessible. – sfdcfox Jul 2 '13 at 17:03
  • sfdcfox, do you know a way around this conflict? Are you aware of a salesforce org having a built in namespace much like apex classes have 'system'? so a query agains system__MyField__c would work even though MyPKG__MyField__c also exists? I know the answer is not 'system', but is there another option? – hemmeter Jan 15 '15 at 23:26
  • Can this conflict exist? I know Salesforce stores the field name and the namespce in separate system fields. – Evan Feb 6 '15 at 19:32
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.