I have a scenario where my columns names can be of form genesis__prod__c/docgen__prod__c/lnd__prod__c,etc depending upon which package is using my solution package. Here genesis, docgen and lnd are different product packages using my solution.

My solution needs to fetch any of these fields (genesis__prod__c/docgen__prod__c/lnd__prod__c) from their respective sObjects in their products.

I want to construct a generic query which will omit the namespace using a wildcard and only look for prod__c in specified sObject. With such a solution, I wouldn't have to hardcode any namespace in my query.

For example, I don't want to form my query like this

String query = 'select Id,Name,docgen__CL_Product__c from '+ sObjectType + ' where id= \'' + appId + '\'';
List<sObject> runtimeDeterminedObject = Database.query(query+' LIMIT 1');

Here I have specifically mentioned docgen__CL_Product__c name to be fetched from a runtime resolved sObject name sObjectType wrt an appId

How can I form a query where I do not have to provide namespace docgen,genesis before prod__c. I do not want to write if-else for each product which can utilize my package.

2 Answers 2


Nope. You can't do it. Every field in your SELECT clause must be one, specific field. You have to loop through the object's field map and pull in the fields individually.

List<String> prodFields = new List<String>();
for (String field : sObjectType.getDescribe().fields.getMap().keySet())
    if (field.contains('__prod__c'))
  • 1
    Thanks. I did the same after searching for a while. I'm looping through the sObject and checking each reference field which is equal to the lookup sObject of *__prod__c and taking that particular apiName given that apiName contains __prod__c similar to your suggestion apiName.contains('__prod__c'). Oct 18, 2016 at 18:09

I am not sure in context of managed package, but if you can get the namespace then you can construct your query based on the namespace of the org.


This approach will work if you are installing a class through your package.

Assume the class name is 'namespace', you can get the raw namespace by using:

nameSpacePrefix =namespace.class.getName().substringBefore('namespace').substringBefore('.');

Look at mattandneil's answer for other ways to get the namespace prefix:

Namespace Prefix in JavaScript and Apex within Installed Package

public class namespace {

    public static string nameSpacePrefix;
    public static String query;
        public namespace(){
         nameSpacePrefix= '';
        public static string constructmysoqlquery(){
           nameSpacePrefix= [SELECT NamespacePrefix FROM Organization].NamespacePrefix;
           // could be handled better by adding the namespaces in a custom setting.
            set<string> namespaceset = new  set<string>{'genesis','docgen','lnd'};
                   query = 'select Id,Name,'+nameSpacePrefix+'_Product__c from '+ sObjectType + ' where id= \'' + appId + '\'';;
        return query;
  • If this is in someone else's org the namespace will be null, no?
    – Adrian Larson
    Oct 18, 2016 at 18:11
  • @Adrian Larson thats the point i was not able to research, this might be open ended unless tested developer.salesforce.com/forums/?id=906F000000092MmIAI
    – Rao
    Oct 18, 2016 at 18:12
  • SELECT NamespacePrefix FROM Organization will bring namespace of the particular package but what if multiple manage packages are installed in the same org. Oct 18, 2016 at 18:13
  • @Cyclotron3x3 I belive then you may have to go for approach where you will have to look for the class installed through your package, I am not an expert with packages but this post seem to tackle your question : salesforce.stackexchange.com/questions/28974/…
    – Rao
    Oct 18, 2016 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .