5

My Apex code needs to dynamically reflect on user-defined Custom Settings. But the user could use a List or Hierarchy depending on their needs, so I need to be a bit careful manipulating each type.

It's ok to differentiate Custom Settings from Custom Objects using isCustomSetting() but I'm struggling to differentiate the two types. I had hoped one would be missing SetupOwnerId or such:

List custom setting has these native fields:

CreatedById
CreatedDate
Id
IsDeleted
LastModifiedById
LastModifiedDate
Name
SetupOwnerId
SystemModstamp

Hierarchy has these native fields... (yep identical, and need to be for sane/happy API)

CreatedById
CreatedDate
Id
IsDeleted
LastModifiedById
LastModifiedDate
Name
SetupOwnerId
SystemModstamp

I suppose we can see that Id == Name on an inserted one. Is there better differentiator?

4

Just found an angle. Starting with the API name:

String api = 'MyCustomSetting__c';

bit of jiggery pokery...

//dynamic object info
Type reflector = Type.forName(api);
SObject sobj = (SObject)reflector.newInstance();
DescribeSObjectResult sobjResult = sobj.getSObjectType().getDescribe();

//some Name field info
SObjectField field = sobjResult.Fields.getMap().get('Name');
DescribeFieldResult fieldResult = field.getDescribe();
Integer length = fieldResult.getLength();

...results seem conclusive enough:

if (length == 38) {
    //it's a list!
} else if (length == 80) {
    //it's a hierarchy!
}

(The length of the Name field differs)

  • 1
    Could you not do a bit of round peg square hole and do a getAll() execution in a try catch block? Only list custom settings have the getAll method so if it fils or throws an error it is Hierarchy. (Not sure if it even throws an error as I did not try it.. Although your way is elegantly complex :) and not as hackish – Eric Aug 10 '15 at 22:57
  • Nice angle @Eric thanks! Struggling to do that dynamically... – bigassforce Aug 10 '15 at 22:58
  • 1
    Not sure if it will work dynamically, it just popped into my head so......have not actually tried it which is why comment and no answer :) – Eric Aug 10 '15 at 23:00
  • @Eric Fairly certain that throws a compile error, which you cannot catch. – Adrian Larson Aug 10 '15 at 23:00
  • @AdrianLarson - Well shoot, yes it does. Oh well... – Eric Aug 10 '15 at 23:03
2

Or also, the hierarchy custom setting will have at least one record where the SetupOwnerId is the company Id

SetupOwnerId = Userinfo.getOrganizationId()

  • This is also the case with List custom settings, at least based on my investigation in Execute Anonymous. – Adrian Larson Aug 10 '15 at 23:09
0

Though @bigassforce provides an interesting way to determine dynamically if custom setting has a list or hierarchy type like checking the length of field name

Integer length = Type.forName(x).newInstance().getSObjectType().getDescribe().Fields.getMap().get('Name').getDescribe().getLength();

if (length == 38) {
    //it's a list!
} else if (length == 80) {
    //it's a hierarchy!
}

were x is the name of custom settings, like List_Custom_Setting__c or 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=0873A000000CZR9QAO and we should vote for it to avoid clumsy code for a such a trivial query

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.