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

  • what is the type of custom setting ? – Himanshu Mar 3 '16 at 8:27
  • Its of List type – Priyalakshmi Mar 3 '16 at 8:31
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.

  • check my updated answer. – Tushar Sharma Mar 3 '16 at 10:00
  • Please check if my answers solves the problem? – AtulRajguru9 Mar 3 '16 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.