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
RyanhRyanh 

Can't test product scheduling?

We've enabled product scheduling on opportunities and defined specific products to have a default scheduling/recurrance profile of monthly for 12 recurrances.

 

I've created a trigger that fires after the OpportunityLineItem object is inserted -- it updates the same OpportunityLineItem record to include the monthly unit price and the number of schedule entries. It works fine.

 

I'm now trying to create a test case for this trigger, but running into problems. My test sets up the following objects, in this order:

 

  1. Account
  2. Opportunity (related to the account)
  3. Product2
    1. CanUseRevenueSchedule=true
    2. RevenueScheduleType='Repeat'
    3. NumberofRevenueInstallments=12
    4. RevenueInstallmentPeriod='Monthly'
  4.  Pricebook2 (a SOQL query to get the Standard Price Book)
  5. PriceBookEntry 
    1. Related to the price book above
    2. Related to the product above
    1. Unit price=0
    2. UseStandardPrice=false
  6. OpportunityLineItem
    1. Related to the opportunity above
    2. Related to the PB entry above
    3. Quantity=1
    4. UnitPrice=200
In the SFDC interface, when you add the Opportunity Product (OpportunityLineItem) and specify the quantity and price, it immediately creates the product schedule and the trigger uses the total price / the number of recurrances to arrive at the unit price per month.
 
However, in the test case, when I insert the OpportunityLineItem record, I get a [Divide by 0] exception because (presumably) the schedule is not getting built automatically before the trigger fires, so the list I've built with the schedule entries returns 0 from the list.size() method.
 
 
I'm new to SFDC and test case writing in general so I fully acknowledge that there may be a flaw in how I've written this test. Whatever the case, if you can spot the problem, I'd appreciate it.
 
Thanks!