+ Start a Discussion
Mohamed BenyahiaMohamed Benyahia 

how to insert new fields in custom setting using Apex

how to insert new record in custom setting object field using Apex
Hii Mohammed
Try Below Code Example:
MyCustomSetting__c myCustomSetting = new MyCustomSetting__c();
myCustomSetting.Field_1__c = 'A';
myCustomSetting.Field_2__c = 5;
insert myCustomSetting;
Please Mark It As Best Answer If It Helps
Thank You!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mohammed,

I hope the question is how to add the custom setting data using apex. Please find the below link which answers the same,


List setting:
upsert new ListSetting__c(
    Name = 'sandbox',
    CustomField__c = 'https://test.salesforce.com/'

upsert new ListSetting__c(
    Name = 'production',
    CustomField__c = 'https://login.salesforce.com/'

I hope we cannot create or add a field to the custom setting using apec. We can only add the data to it.

If this solution helps, please mark it as best answer.

ravi soniravi soni
Hi Mohamed,
custom setting is like an custom object. you can create a field like custom object but if you want to create custom setting data from apex, below is example.
TestCustomSetting__c oCustomSetting = new TestCustomSetting__c();
oCustomSetting .Field_1__c = 'A';
oCustomSetting .Field_2__c = 5;
//Like this you can fill all fields
 insert oCustomSetting ;
don't forget to mark it as best answer if it helps you.
Thank you
Santoshi K VSantoshi K V
Hi Mohamed

Like custom objects , custom settings also have name as required field by default which can later be used to get data as key value pair Hence It is
important to provide name to custom setting while creating a record .
Following code should work for you if you have no other required field other than name .
API_KEY__c ak=new API_KEY__c(Name= 'TESTKEY1',keys__c = 'Test1'); insert ak;

Please mark it best answer if it solves.