function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chaithaly gowdachaithaly gowda 

How to access custom setting in the order?

I have a custom setting 'Licensename__c' where 'license__c' is the only field. I am using the below logic to access the data from it 

List<LicenseName__C> names = LicenseName__c.getall().values();
system.debug(names[0].License__c);
system.debug(names[1].License__c);
system.debug(names[2].License__c);
system.debug(names[3].License__c);

The debug statements are not giving the data in the order that i have entered in custom setting.

Is there any way to bring the data in the code in the same order i entered in custom setting!?

Also please let me know how to count the number of dataset in the custom setting.
Any help is appreciated.

Thank you!
Ravi Dutt SharmaRavi Dutt Sharma
Hi Chaithaly,

Using the custom settings methods, you cannot get the data in the order in which you have entered. The order may not be guaranteed. Can you please explain your use case in terms of why you have a dependency on the order of the records returned by getall() method?

To answer your second question to count the number of dataset, you can check the size of the list returned by getall().values() method. This will give you the count of records available in your custom setting.
 
List<LicenseName__c> names = LicenseName__c.getall().values();
Integer countOfElements = names.size();

Thanks,
Ravi
 
sachinarorasfsachinarorasf
Hi chaithaly,

Please use the below code to access custom setting data in the same order in which order you entered in a custom setting

List<LicenseName__C> names = new List<LicenseName__C>();
names = [Select License__c FROM  LicenseName__C ORDER BY CreatedDate];
system.debug(names[0].License__c);
system.debug(names[1].License__c);
system.debug(names[2].License__c);
system.debug(names[3].License__c);

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com