2

I have a requirement to avoid any compile-time dependency on a managed package. Dynamic SOQL, SObjectType and SObject allow me to do this cleanly for normal objects.

But now I need to access a public hierarchy custom setting from the managed package. With compile-time dependency this would work:

ns__CustomSetting__c setting = ns__CustomSetting__c.getInstance();

But there does not appear to be a dynamic alternative API; methods like getInstance() are not present on SObject. Or have I missed something?

Is the only approach available to use dynamic SOQL to query the various rows of the hierarchy and emulate the semantics of getInstance() in Apex code?

4

You can access custom settings dynamically through a query. Unfortunately, this means that you'll have to burn a query and at least one query row (and up to 3). First, you'll get the user's organization ID, profile ID, and user ID, then perform a query like: SELECT Id, SetupOwnerId FROM CustomSetting__c WHERE SetupOwnerId IN (:userId, :profileId, :orgId). Then, you have to go through each field of each returned row (if any), and override higher level settings with lower level settings.

Map<SObjectType, Setting__c> settings = new Map<SobjectType, Setting__c>();
for(Setting__c record: [SELECT Id, SetupOwnerId... FROM Setting__c WHERE ...])
    settings.put(record.setupownerid.getsobjecttype(), record);

if(settings.containskey(organization.sobjecttype)) {
    ...
}
if(settings.containskey(profile.sobjecttype)) {
    ...
}
if(settings.containskey(user.sobjecttype)) {
    ...
}

There's no other way to access those settings. Also, you can't access protected settings outside the package.

  • Clarification: I think that you meant if(settings.containskey(profile.sobjecttype)) { on line 8 – zachelrath Mar 25 '14 at 18:53
  • I did. Good catch. – sfdcfox Mar 26 '14 at 2:53
0

Though we can dynamically get custom settings records by a dynamic query

Database.query('SELECT Id, SetupOwnerId FROM ' + x + ' WHERE SetupOwnerId IN (\'' + UserInfo.getUserId() + '\',\'' + UserInfo.getProfileId() + '\',\'' + UserInfo.getOrganizationId() + '\')')

were x is the name of custom settings, like Hierarchy_Custom_Setting__c, I think we should instead open an idea for Salesforce to implement an appropriate method for that like this one https://success.salesforce.com/ideaView?id=0873A000000CZREQA4 and we should vote for it to be able to get Hierarchy custom settings records dynamically without burning SOQL query

  • In my experience developer oriented ideas don't get implemented so possibly wasted effort. – Keith C Apr 18 at 13:53
  • @KeithC At least we can call schema method to get record type id by Developername, this is something. Except for this rare case exception I totally agree with you but we shouldn't give up. – Patlatus Apr 19 at 10:11

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.