3

I'm creating lookup fields on an object dynamically using the Metadata API, saving the name of a field as a Custom Setting and building queries dynamically. This works perfectly, but there is a conflict with the same name fields which are already there with a namespace prefix.

Let's say I have an object in my package called prefix__Transaction__c with a field prefix__Subscription__c.

For instance, I am building a query like this:

String q = 'SELECT ' 
+ Transaction__c.Subscription__c.getDescribe().getName()
+ ','
+ Transaction__c.SObjectType.getDescribe().fields.getMap().get('Subscription__c')
+ 'FROM etc etc etc';

(I already created in the org where the package is installed a field called Subscription__c)

I expect to have something like this:

SELECT prefix__Subscription, Subscription__c FROM etc etc etc

However what I'm getting is this:

SELECT prefix__Subscription, prefix__Subscription__c FROM etc etc etc

i.e. a duplicate field in the query. The system thinks on Transaction__c.SObjectType.getDescribe().fields.getMap().get('Subscription__c')

that I'm interested in the namespaced Subscription__c field, however I want to get the un-namespaced one.

Do anybody have an idea have to do this? Please note that if there is NO conflicts between the namespaced and un-namespaced field names (a field name like 'A_Completely_New_Lookup__') everything works as intended, so the code from the package can get the fields which are created in the org.

5

In my experience, the partial solution is to eliminate any duplicate field names via a set when creating a dynamic query of this form. This avoids an error when the query runs, but you will only get access to the namespaced version of the field not the locally added non-namespaced field.

It is an unfortunate limitation of the platform at the moment that you must choose distinct (ignoring the namespace prefix) API names for your locally added fields to avoid this problem. Those fields can of course have whatever labels you like.

I assume this problem originates from the logic that allows queries inside a managed package to be written without the namespace prefix. I recollect that the describe call results changed a few releases back to include the field namespace prefixes but a corresponding change - to allow namespaced and non-namespaced fields to be identified separately - has not been made in the query API.

Suggest you comment/vote on this idea Support Dynamic SOQL "No Shadowing" Option.

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.