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
jessie dongjessie dong 

Is there any way to create custom setting programmatically?

Hi,
Instead of create the custom settings manually from Setup-->Develop --> Custom Settings -->New , anyone know is any  way to create them automatically in Apex class?
thanks in advanced!
 
Best Answer chosen by jessie dong
DevADSDevADS
Hey Jessie,

There is no any automated mechanism to create custom setting records dynamically. You can either create a generic method which would check if there are any custom setting record and if is empty then perform a DML operation and insert it.

In one of my project, we created JSON files and created a generic logic which would parse the JSON and create custom setting record if those are missing.

Else, I would recommend to switch over to Custom Metadata Types that would help you to deploy in between environments.

Hope this helps.

Post further here if you have any other questions.

Happy Coding!!
 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Jessie,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://developer.salesforce.com/forums/?id=906F0000000kJURIA2

https://salesforce.stackexchange.com/questions/24424/populating-custom-settings-field-using-apex

https://developer.salesforce.com/forums/?id=906F000000093d5IAA

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
jessie dongjessie dong
Thanks for you quick response. But what I need is to setup the custom settings automatically, in my application, I implement insert/delete/update recodes into exist custom settings which I create manually by Apex codes.
My scenario is when my application starting, it will run a basic check, if the custom settings is not ready, it’s trigger some thread to  setup the custom settings programmatically.
 
DevADSDevADS
Hey Jessie,

There is no any automated mechanism to create custom setting records dynamically. You can either create a generic method which would check if there are any custom setting record and if is empty then perform a DML operation and insert it.

In one of my project, we created JSON files and created a generic logic which would parse the JSON and create custom setting record if those are missing.

Else, I would recommend to switch over to Custom Metadata Types that would help you to deploy in between environments.

Hope this helps.

Post further here if you have any other questions.

Happy Coding!!
 
This was selected as the best answer
jessie dongjessie dong
Thank you a lot. it's very helpful.