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
SalesRedSalesRed 

System.InvalidParameterValueException: Invalid SetupOwner for Custom Settings

Hi,  I have a hierarchial custom setting of which I create an instance of in a test class. I receive an error though when I execute my test class.  The error being.
 "System.InvalidParameterValueException: Invalid SetupOwner for Custom Settings"  

In accessing the custom setting in my Apex class I use the following.
Brokerage__c  brokerage  = Broker__c.getInstance(UserInfo.getOrganizationName());"

The functionality does execute ok however the failute occurs as mentioned in my test class.  Does anyone know I can get around this.  I would have thought the user running the test would exist within the same organisation so it should work.

Any tips or help on this would be appreciated.

Thanks in advance.
Best Answer chosen by SalesRed
SalesRedSalesRed
Hi Avilion,  Thanks for your help.  As mentioned above I had noted the issue was not existing in the test classes alone.  The error cuse was due to one of my classes using the name of the organisation in the getInstance function directly instead of using getInstance(UserInfo.getOrganizationId());
When I changed this it works ok. 

Thank you all the same for your help.  It's good to know it's resolved.

All Answers

SalesRedSalesRed
Actually on running the functionality again, not from my test class it no longer works in general also.  I'm pretty sure it did previously.  Does anyone know how best I can get around this "Invalid SetupOwner for Custom Settings" error?

Thanks.
AshwaniAshwani
Do not getInstance of org custom setting instead create new custom setting record in you test class. You can define heirrarchy custom setting in test class also do not forget to provide correct setupOwnerId value.
SalesRedSalesRed
Hi Avilion,  Thanks for your help.  As mentioned above I had noted the issue was not existing in the test classes alone.  The error cuse was due to one of my classes using the name of the organisation in the getInstance function directly instead of using getInstance(UserInfo.getOrganizationId());
When I changed this it works ok. 

Thank you all the same for your help.  It's good to know it's resolved.
This was selected as the best answer