I am trying to retrieve a custom setting that I made with this code:

    public static String getCustomKey(){
        AP_Mobile_Key__c APMobileKey = AP_Mobile_Key__c.getInstance('AP Mobile Key');
        String myCCVal = APMobileKey.Key__c;
        if(APMobileKey != null){
          return myCCVal;
        return 'No key';

When I try to run it, i get error: Invalid SetupOwner for Custom Settings: AP Mobile Key

These settings exist, I just created them. I cant see to find any reference to this error using Google.

  • 1
    Can you confirm that the custom settings is of type Hierarchy ? if yes, do check the answer on this existing post: salesforce.stackexchange.com/questions/233827/…
    – Shamina
    Jul 22 '19 at 19:33
  • @Shamina Changing it to list type removes the error but now its null when I do: AP_Mobile_Key__c APMobileKey = AP_Mobile_Key__c.getInstance('AP Mobile Key'); Even though these settings are present. Jul 22 '19 at 23:17
  • 1
    Are you sure the value 'AP Mobile Key' matches exactly the "Name" field value in your custom settings? also check if the value 'AP Mobile Key' is correct and there are no spaces in between the actual value in the custom setting
    – Shamina
    Jul 23 '19 at 6:45
  • I never add any data to it it's fixed thanks Jul 23 '19 at 13:49
  • Alright. Can you mark my answer below as acceptable then; I have posted a formal answer to recap on how to fix this issue
    – Shamina
    Jul 24 '19 at 10:37

Just to recap how this was fixed:

The error "Invalid SetupOwner for Custom Settings: AP Mobile Key" was due to the custom settings being hierarchical and getinstance was being used incorrectly on Name value, instead of organisation, user or profile id (Refer to more detailed answer in similar question here: Custom Setting providing NULL value)

(If applicable) When changing the custom settings into List type, ensure that call to getinstance method is based on Name value, and that this has corresponding record data

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.