It would seem that it is not possible to support picklists enabled and picklists not enabled from the same Apex Code if we are writing to the State or Country. Consider the following:
oAccount[0].BillingStreet = '1060 W Addison St';
oAccount[0].BillingCity = 'Chicago';
oAccount[0].BillingPostalCode = '60613-4566';
oAccount[0].BillingState = 'IL';
oAccount[0].BillingCountry = 'US';
update oAccount;
This code works perfectly if picklists are not enabled. When picklists are enabled, the fields BillingState and BillingCountry become read only and two new fields are added BillingStateCode and BillingCountryCode which are meant to contain the ISO 2 char code.
Because the State and Country fields are changed to read only, the above code fails if picklists are enabled with a runtime exception on the update. In order to support picklists, we can make the following modificaiton:
oAccount[0].BillingStreet = '1060 W Addison St';
oAccount[0].BillingCity = 'Chicago';
oAccount[0].BillingPostalCode = '60613-4566';
oAccount[0].BillingStateCode = 'IL';
oAccount[0].BillingCountryCode = 'US';
update oAccount;
This code works fine on orgs with picklists enabled, but will not compile or run on orgs where they are not, because the fields BillingStateCode and BillingCountryCode do not exist.
The problem is how to support both from the same code? It would appear that it cannot be done. We can certainly detect whether or not picklists are enabled as follows:
private boolean PickListsEnabled()
{
// if Account Billing Address is enabled, assume all addresses are enabled
Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();
return M.containsKey('BillingStateCode');
}
and then try something like:
oAccount[0].BillingStreet = '1060 W Addison St';
oAccount[0].BillingCity = 'Chicago';
oAccount[0].BillingPostalCode = '60613-4566';
if(PickListsEnabled())
{
oAccount[0].BillingStateCode = 'IL';
oAccount[0].BillingCountryCode = 'US';
}
else
{
oAccount[0].BillingState = 'IL';
oAccount[0].BillingCountry = 'US';
}
update oAccount;
But on orgs that do not have picklists enabled, this still will not compile or run due to the mention of the BillingStateCode and BillingCountryCode fields - even if they will not be accessed in the case that picklists are enabled. If we precompile our managed package on an org with picklists enabled, it will not install on orgs where they are not enabled.
So is there no way to support both from the same codebase? Will we be forced to maintain 2 different versions of our managed package? Any suggestions are appreciated.
Suggest SalesForce consider implementing this in just the opposite way. Leave the BillingCountry and BillingState fields as they are, and the new fields are called BillingCountryFull and BillingStateFull and are read only. In this scenario, for example, United States would be the value of BillingCountryFull when BillingCountry has the value "US" written to it.