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.