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
Alexandra StehmanAlexandra Stehman 

Why is this trigger throwing an error on "Invalid SetupOwner for Custom Settings"?

First and foremost - THANK YOU for reading this.  I've been trying numerous ways to make this work and have even consulted with the guys in the office.  No one is quite sure what's going on. Online - can't find a lot of information on this, just one singular post that doesn't help me out a ton. 

What I'm trying to do is: modify the price of a Product (that has a schedule set on it) prior to insert on the Opportunity, in order to enforce a minimum price on the Product. Why am I doing it this way? Before setting the schedule on the product, a workflow rule was successfully enforcing the monthly minimum price on the product, using a custom setting.  After setting the schedule on the product,the workflow rule no longer worked. Trying to put a trigger on the OpportunityProduct object, before insert, to get around this.  (If anyone can tell me why the workflow rule stopped working once I set the schedule on the Product, and if it can be worked around, I'd gladly scrap this trigger route.)

When I attempt to add the qualifying product to the Opportunity, it throws this error:
Apex trigger EnforceMinimumPrice caused an unexpected exception, contact your administrator: EnforceMinimumPrice: execution of BeforeInsert caused by: System.InvalidParameterValueException: Invalid SetupOwner for Custom Settings: Price_Limits: Trigger.EnforceMinimumPrice: line 4, column 1 

The trigger code (note on line 4, I've also tried  getInstance('Price_Limits'):
trigger EnforceMinimumPrice on OpportunityLineItem (before insert) {
    for (OpportunityLineItem oli :  Trigger.new)    
    {
        Price_Limits__c priceLimits = Price_Limits__c.getInstance('Price Limits');
        Decimal MIN_MONTHLY_FEE = priceLimits.Min_Mnthly_Fee__c;
        if ( (oli.Product2.Name.equals('Hardcoded Product Name')) && 
             (oli.Quantity == 1) && 
             (oli.UnitPrice < MIN_MONTHLY_FEE)
           )
        {            oli.UnitPrice = MIN_MONTHLY_FEE;           }    } }

Here is an (obfuscated) screenshot of the custom settings:
Custom Settings Screen Shot

Thank you!
Alexandra StehmanAlexandra Stehman
OK, problem solved. Had to pass in an owner id into the custom setting.  Was looking at the wrong code example (list type not hierarchical).