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
34213421 

insert opportunity line item schedule in test class

I am unable to insert opportunity line item schedule record in the test class. Can anyone let me know the fields and criteria for inserting schedule record in test class
 
Narender Singh(Nads)Narender Singh(Nads)
Hi, 
To insert an opportunity line item there are two mandatory fields:
OpportunityId
PricebookEntryId

Note: You will have to first create a PricebookEntry type record in your test data, if you are not setting SeeAllDate=True.

Let me know if it helps
Thanks
Ajay K DubediAjay K Dubedi
Hi,
This is Test Class for opportunityLineItem.
I think this will help you to write the test Class.

@istest
public class ExtensionTestclass{
    
    static testmethod void  extensionTestMethod(){
      //Create opportunity
        opportunity oppr = new opportunity();
        oppr.Name='testing Opp';
        oppr.StageName = 'Prospecting';                         
        oppr.CloseDate = Date.today();
        insert oppr;
        //Use Standard price Book
        Id PriceBookId = Test.getStandardPricebookId();
        
        //Create Product
        Product2 pro2 = new Product2();
        pro2.Name='BXCD';
        pro2.ProductCode='BXCD24';
        pro2.isActive=true;
        insert pro2;
        
        //Create PriceBook Entry
        PricebookEntry pbe =new PricebookEntry();
        pbe2.unitprice=1.0;
        pbe2.Product2Id=pro2.Id;
        pbe2.Pricebook2Id=PriceBookId;                                     
        pbe2.isActive=true;
        insert pbe;
        //Create OpportunityLineItem
        OpportunityLineItem OPplineitem2 = new OpportunityLineItem ();
        OPplineitem2.Quantity=2;
        OPplineitem2.OpportunityId=oppr.Id;
        OPplineitem2.UnitPrice=1.0;
        OPplineitem2.Product2Id=pro2.Id;
        OPplineitem2.PriceBookEntryId=pbe.Id;
        insert OPplineitem2;
    }
   
}

Please mark it as best if you find it helpful.

Thank You
Ajay Dubedi
 
34213421
Hi,

I am looking at inserting the line item schedule record, not the line item

Thanks,
Harisha 
Narender Singh(Nads)Narender Singh(Nads)
Hi Harisha,

Refer this link: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunitylineitemschedule.htm

The document is self explanatory.

Let me know if you need any further clarification.
Thanks!
Ajay K DubediAjay K Dubedi
Hi Harisha,

To make it visible you have to follow below steps:
Opportunity Line Item Schedules are only visible after you activated the feature at the Org in question:

Setup > Customize > Products > Product Schedules Settings 

Once activated, there is a new Standard Object called OpportunityLineItemSchedule with a lookup reference to the
OpportunityLineItem by the field OpportunityLineItemId. 
This object can be manipulated in APEX with the usual DML operations.

I hope you will understand this.

Thank You
Ajay Dubedi
Akshay Patil 140Akshay Patil 140
To Create OpportunityLineItemSchedule record for Opportuinity firstly enable Product Schedules Settings for Quantity from here 
Setup > Customize > Products > Product Schedules Settings 

 Product2 p3 = new Product2(Name='amit',IsActive =true,MRP__c = 120);    
 p3.CanUseQuantitySchedule = true;  
 p3.IsActive = true;  
 insert p3;

priceBookEntry priceBkEntry=[select id from priceBookEntry where Product2Id=:p3.Id limit 1];  
OpportunityLineItem OPplineitem2 = new OpportunityLineItem(Quantity=25,
UnitPrice=543,
OpportunityId=oppr.Id,
PriceBookEntryId=priceBkEntry.Id);        
insert OPplineitem2;  
 OpportunityLineItemSchedule OpSchedule= new OpportunityLineItemSchedule         (Type='Quantity',
OpportunityLineItemId=OPplineitem2.Id,
Quantity=3,
ScheduleDate=Date.today()+2);
insert OpSchedule;