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
Karimov CeyhunKarimov Ceyhun 

Custom setting objects creation modification through apex code

Hi,

 

I am new to force.com applications. I am supposed to use custom settings objects in my application. When i looked some examples and tutorials, i saw that only getter methods are used in custom setting objects in apex code. So is there any way to create some custom settings objects, modify their fields, insert new data to its fields through apex code?

 

 

Thanks

 

Ceyhun Karimov

Best Answer chosen by Admin (Salesforce Developers) 
RArunrajRArunraj

Hi Ceyhun,

 

Using Apex for custom setting Insert, Update and delete

 

For insert


samplelist__c sList = new samplelist__c(name='xxxxxxxxxxxx');

insert sList;

 

For Update


You can do a query to get the id or you can use the below code to get the id

 

samplelist__c s = samplelist__c.getInstance('xxxxxxxxxxxx');

String sampleListId = s.id;


Currently I have hardcoded the id

 

samplelist__c sList = new samplelist__c(id='a0190000000Zrai',name='xxxxxxxxxxxx');

update sList;


For delete


samplelist__c s = [select id,name from samplelist__c where name='xxxxxxxxxxxx'];delete s;

 

or


List<samplelist__c> mcs = samplelist__c.getall().values();

delete mcs;

 

Thanks,

Arunraj

All Answers

RArunrajRArunraj

Hi Ceyhun,

 

Custom setting is like an object, In that you can create fields and add values for those fields.

 

Custom settings can be created by clicking Develop --> Custom Settings --> New --> Give the name of the custom setting object.

 

Then create new fields in the custom setting object detail page by clicking New on the custom fields section

 

Then by using Manage button in the custom setting object detail page and then click New.

 

Thanks,

Arunraj

 

 

Karimov CeyhunKarimov Ceyhun

Thanks for the reply,

 

I didnt mean that.

I meant to create custom settings from apex code not manually just you have said.

 

 

RArunrajRArunraj

Hi Ceyhun,

 

Using Apex for custom setting Insert, Update and delete

 

For insert


samplelist__c sList = new samplelist__c(name='xxxxxxxxxxxx');

insert sList;

 

For Update


You can do a query to get the id or you can use the below code to get the id

 

samplelist__c s = samplelist__c.getInstance('xxxxxxxxxxxx');

String sampleListId = s.id;


Currently I have hardcoded the id

 

samplelist__c sList = new samplelist__c(id='a0190000000Zrai',name='xxxxxxxxxxxx');

update sList;


For delete


samplelist__c s = [select id,name from samplelist__c where name='xxxxxxxxxxxx'];delete s;

 

or


List<samplelist__c> mcs = samplelist__c.getall().values();

delete mcs;

 

Thanks,

Arunraj

This was selected as the best answer
Karimov CeyhunKarimov Ceyhun

Thanks for the reply RArunraj,

 

 

I understood the problem. It worked fine. However another problem arises. 

 

I modified an countryStatePicker example given in http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm.

 

I hava just put a button, when i press that button a new element is created and inserted as you have explained.  The insertion works fine, to exception no  error.  But when page reloads after i press the button, i cannot see the newly inserted record. If i press button second time(so insert the record second time) it gives me en exception that record already exists(so the record is inserted, and the exception is normal).. i cannot understand the reason...

 

 

 

so i added this part to visualforce page to the example:

 

<apex:commandbutton value="submit" action="{!submit}"/>

 

 

and added this part to apex code:

 

public void submit()
    {
        Foundation_States__c koko = new Foundation_States__c(    Name = 'az',Country_Code__c = '11');          
        insert(koko);    
       
    }

 

 

insertion is done fine. But when the page reloads, the getCountriesSelectList() function is called as given in example, 

 

public List<SelectOption> getCountriesSelectList() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', '-- Select One --'));       

        // Find all the countries in the custom setting 
   
        Map<String, Foundation_Countries__c> countries = Foundation_Countries__c.getAll();
       
        // Sort them by name 
    
        List<String> countryNames = new List<String>();
        countryNames.addAll(countries.keySet());
        countryNames.sort();
       
        // Create the Select Options. 
    
        for (String countryName : countryNames) {
            Foundation_Countries__c country = countries.get(countryName);
            options.add(new SelectOption(country.country_code__c, country.Name));
        }
        return options;
    }

 

 

but i cannot see my inserted record..

 

Am i doing something wrong or something incomplete?

 

Thanks

 

Ceyhun Karimov

Karimov CeyhunKarimov Ceyhun

Problem is solved

 

there was a little typo..

 

Sorry for inconvinience..

 

May it be easy

 

Ceyhun