0

I am setting up some test data for my unit test but am hitting the wall when inserting a OpportunityLineItemSchedule.

Here's my code:

@IsTest
static void myTest() {
    Account acct = new Account();
    acct.Name = 'Test Account';
    insert acct;

    User adminUser = new User();
    adminUser.FirstName = 'Test';
    adminUser.LastName = 'Test';
    adminUser.Alias = 'Test';
    adminUser.Email = '[email protected]';
    adminUser.UserName = '[email protected]';
    adminUser.EmailEncodingKey = 'UTF-8';
    adminUser.LanguageLocaleKey = 'en_US';
    adminUser.LocaleSidKey = 'en_US';
    adminUser.TimezoneSidKey = 'America/Los_Angeles';
    adminUser.ProfileId = [
                SELECT Id
                FROM Profile
                WHERE Name = 'System Administrator'
        ].Id;
    insert adminUser;

    Opportunity opp = new Opportunity();
    opp.Name = 'Test';
    opp.AccountId = acct.Id;
    opp.StageName = 'Closed Won';
    opp.CloseDate = Date.today().addDays(30);
    insert opp;

    Pricebook2 pbook = new Pricebook2();
    pbook.Name = 'Test Pricebook';
    pbook.IsActive = true;

    Product2 prod = new Product2();
    prod.Name = 'Test Product';
    prod.IsActive = true;
    insert prod;

    PricebookEntry standardPricebookEntry = new PricebookEntry();
    standardPricebookEntry.Pricebook2Id = System.Test.getStandardPricebookId();
    standardPricebookEntry.Product2Id = prod.Id;
    standardPricebookEntry.IsActive = true;
    standardPricebookEntry.UnitPrice = 50;
    insert standardPricebookEntry;

    PricebookEntry pbookEntry = new PricebookEntry();
    pbookEntry.Pricebook2Id = pbook.Id;
    pbookEntry.Product2Id = prod.Id;
    pbookEntry.IsActive = true;
    pbookEntry.UnitPrice = 100;
    insert pbookEntry;

    OpportunityLineItem oli = new OpportunityLineItem();
    oli.UnitPrice = 100;
    oli.Quantity = 10;
    oli.OpportunityId = opp.Id;
    oli.PricebookEntryId = pbookEntry.Id;
    oli.ServiceDate = Date.today();
    insert oli;

    OpportunityLineItemSchedule lineItemSchedule = new OpportunityLineItemSchedule();
    lineItemSchedule.Revenue = 10;
    lineItemSchedule.OpportunityLineItemId = oli.Id;
    lineItemSchedule.Type = 'Revenue';
    lineItemSchedule.ScheduleDate = Date.today();
    insert lineItemSchedule;
}

The error I am getting is:

FIELD_INTEGRITY_EXCEPTION, field integrity exception: unknown (scheduling not enabled on product): [unknown]

How do I programatically enable the scheduling. I am having a hard time figuring this one out.

1 Answer 1

0

I guess I overlooked the documentation.

Here's a link:

The Product2 on which the OpportunityLineItem is based must have the appropriate CanUseRevenueSchedule or CanUseQuantitySchedule fields (or both) set to true.

All I had to do is add one more line of code being prod.CanUseRevenueSchedule = true; and the issue was resolved.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .