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
Kirill_YunussovKirill_Yunussov 

How to create Custom Settings using Apex?

I only know how to create these via UI, and am having trouble finding this info on the internet.  Please and thanks

 

Needed for:  

1) Have multiple developer/sandbox orgs, and need to create custom settings in each using an apex script, vs manually via UI.

2) Test Method best practice:  to use custom settings you have to use the seeAllData=true, OR you have to create these Custom Settings in your test class.  Is that even possible and how do you do that? 

 

Also, let me clarify - I am asking not about instances of existing custom settings, but  how to create an actual new Custom Settings classes/types programmatically.

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs

@Kirill - Here goes a stab at your queries:
1. The custom settings are treated as custom objects and count against org settings. In order to create new custom settings across orgs the standard practise would be to use change sets or ant to migrate these programatically or on a scheduled basis. The values against the fields are also treated as standard data and you can use DML statments (insert/update/upsert) to manage/migrate the data using apex anonymous blocks or scheduling.

2. Based on the above custom setting data is like any other org data and will have to be manually created in the test class or accessed using the annotation isTest (SeeAllData=true). As a best practise for writing any test we need to manually setup all data in the test and not rely on existing org data. This means limiting use of "SeeAllData" (except in emergencies I guess.

All Answers

souvik9086souvik9086

Please check this

 

http://help.salesforce.com/help/doc/en/cs_accessing.htm

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Kirill_YunussovKirill_Yunussov

Thanks, but it doesn't say how to create Custom Setting classes via apex.

vbsvbs

@Kirill - Here goes a stab at your queries:
1. The custom settings are treated as custom objects and count against org settings. In order to create new custom settings across orgs the standard practise would be to use change sets or ant to migrate these programatically or on a scheduled basis. The values against the fields are also treated as standard data and you can use DML statments (insert/update/upsert) to manage/migrate the data using apex anonymous blocks or scheduling.

2. Based on the above custom setting data is like any other org data and will have to be manually created in the test class or accessed using the annotation isTest (SeeAllData=true). As a best practise for writing any test we need to manually setup all data in the test and not rely on existing org data. This means limiting use of "SeeAllData" (except in emergencies I guess.

This was selected as the best answer
Kirill_YunussovKirill_Yunussov

Great, I just saw that they are custom objects, and can be deployed via ant.  We are using Jenkins/subversion