3

I am trying to insert values to the field in custom settings via visual force page

Page:

<apex:page controller="sample" sidebar="false" > <apex:form >
    <apex:pageblock >
        <apex:inputText value="{!myValueFromPage}"/>
            <apex:commandButton action="{!sample}" value="Save Input"/>
       </apex:pageblock> </apex:form> </apex:page>

Class:

public class sample
{
public String myValueFromPage{get; set;}

public void sample() {
User user = [Select Id from User where Name='warner'];
    garage_app__saver__c settings = garage_app__saver__c.getInstance();
    settings.name = myValueFromPage;
   settings.SetupOwnerId=user.Id;
    insert settings;
}



}

I am getting the following error

Error:

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: SetupOwnerId (id value of incorrect type): [SetupOwnerId]

I want to know where I am going wrong..Thanks in advance

2
  • what is the type of custom setting ?
    – Himanshu
    Commented Mar 3, 2016 at 8:27
  • Its of List type Commented Mar 3, 2016 at 8:31

1 Answer 1

4

Try this.

 CustomerSetting__c settings = CustomerSetting__c.getInstance();
    settings.SomeInfo__c = 'test';
    settings.name = 'test';
    settings.SetupOwnerId  = UserInfo.getOrganizationId();
insert settings;

OR your code could be written like:

garage_app__saver__c settings = garage_app__saver__c.getInstance();
settings.name = myValueFromPage;
settings.SetupOwnerId = UserInfo.getOrganizationId();;
insert settings;

Interestingly SFDC documentation says its USER ID (hirachy type setting) but it does not work when we pass USER ID.

If no custom setting data is defined in the hierarchy, the returned custom setting has empty fields, except for the SetupOwnerId field which contains the user ID. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm

To find the solution I just entered data via UI and queried it with workbench.

Then i realized it is ORG ID and not USER ID.

2
  • check my updated answer. Commented Mar 3, 2016 at 10:00
  • Please check if my answers solves the problem? Commented Mar 3, 2016 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .