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
kaushik Sabapathykaushik Sabapathy 

test class for inserting new opportunityLineItem record based on inserting

Hi,
Im a beginner in Saleforce. Need help in creating test class  for opportunityLineItem(for inserting values in opportunityLineItem).
I had a scenario in which i have to calculate TAV based on different input. I have created a apex class for it  and it is  working fine.
I have used custom setting in getting the details
​Renewal :Subscription;MRR;Credit;Monthly;Pre-Pay;No-change;Upgrade;Downgrade;Reprice;Product Change
second value:Hardware;Software;Appliance;SW/HW
third value:Support and Maintenance;S&M:Annual
Public Static void tavcal1()
{
if(isCalculated==false){
String result;
List<OpportunityLineItem> direct2=[SELECT Product_Type__c,Line_Transaction_Type__c,Monthly_Net_Price__c,TAV_Category__c,TAV__c,Term__c,Interval__c FROM OpportunityLineItem where Opportunity.Type='Renewal'];
  Map < String, OpportunityLine__c> customSettingsMap =OpportunityLine__c.getAll();
           String filterStages = customSettingsMap.get('Renewal').Product_Type__c;
           String secondStages=customSettingsMap.get('second value').Product_Type__c;
           String thirdStages=customSettingsMap.get('third value').Product_Type__c;
//           CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);

       System.debug('Filtered Stages == '  + filterStages);
       System.debug('Filtered Stages == '  +secondStages );
         
         
         for(OpportunityLineItem opp:direct2)
         {
        try
        {
         if(Trigger.isUpdate||Trigger.isInsert)
         {
         OpportunityLineItem oldOpp = (OpportunityLineItem)Trigger.oldMap.get(opp.Id);
          
                                       
         if(filterStages.contains(opp.Product_Type__c)&&filterStages.contains(opp.TAV_Category__c)&&filterStages.contains(opp.Interval__c)&&filterStages.contains(opp.Line_Transaction_Type__c)){
         if(opp.Term__c<=12)
         {
         opp.TAV__C=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*opp.Term__C;
if(opp.TAV__C<0)
opp.TAV__C=0;
}
 
         else
         {
         if(opp.Term__c<=12)
         {
         opp.TAV__c=(opp.Monthly_Net_Price__c-oldOpp.Monthly_Net_Price__c)*12;
         if(opp.TAV__C<0)
opp.TAV__C=0;
         }
          }
                       }
         else  if(secondStages.contains(opp.Product_Type__c)&&secondStages.contains(opp.TAV_Category__c)&&(opp.Interval__c==null))
         {
        
         opp.TAV__c=opp.Monthly_Net_Price__c;
         }
         else 
         {
         if(thirdStages.contains(opp.Product_Type__c)&&thirdStages.contains(opp.TAV_Category__c)&&thirdStages.contains(opp.Interval__c)){
         if(opp.Term__c>12)
          opp.TAV__c=((opp.Monthly_Net_Price__c *36) / 0.48) * 0.2;
         else 
          opp.TAV__c=opp.Monthly_Net_Price__c;
          }
         }
                      
         
         }
         
         }
         catch (System.NullPointerException e) {

}
         isCalculated = true;
         update opp;
         }   
         
         
}/* end of for loop*/            
} /*end of if*/    
I dont know how to insert opportunity line item as it was asking product id to be inserted
 i have tried to create a Product id but was not successful
Please suggest to insert a new record on opportunityLineItem



Regards,
Kaushik 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
You need to create PriceBookEntry record and then set the PriceBookEntryId field on OpportunityLineItem
Suraj Tripathi 47Suraj Tripathi 47
Hi Kaushik,

"You can write a test class for OpportunityLineItem by this way."
@isTest
public class CloneOpportunityTest {
    @isTest
    static void cloneOpp()
    {
        Opportunity oppName=new Opportunity();
        oppName.Name='Opportunity1';
        oppName.CloseDate=date.today();
        oppName.StageName='Closed Won';
        insert oppName;
        
        Product2 proInstance=new Product2();
        proInstance.Name='Laptop';
        proInstance.IsActive=true;
        insert proInstance;
        
        Pricebook2 standardPricebook = new Pricebook2(Id = Test.getStandardPricebookId(),IsActive = true);
        update standardPricebook;
        
        PriceBookEntry priceBook1=new PriceBookEntry();
        priceBook1.Pricebook2Id=standardPricebook.id;
        priceBook1.Product2Id=proInstance.Id;
        priceBook1.UnitPrice=100;
        priceBook1.IsActive=true;
        insert priceBook1;
        
        OpportunityLineItem oppLineItem=new opportunityLineItem();
        oppLineItem.PricebookEntryId=priceBook1.Id;
        oppLineItem.OpportunityId=oppName.Id;
        oppLineItem.UnitPrice=100;
        oppLineItem.Quantity=10;
        insert oppLineItem;
        
        Opportunity oppNameClone=new Opportunity();
        oppNameClone.Name=oppName.Name+'clone';
        oppNameClone.CloseDate=date.today()+30;
        oppNameClone.StageName=oppName.StageName;
        insert oppNameClone;
        
        OpportunityLineItem oppLineItemClone=new opportunityLineItem();
        oppLineItemClone.PricebookEntryId=priceBook1.Id;
        oppLineItemClone.OpportunityId=oppNameClone.Id;
        oppLineItemClone.UnitPrice=100;
        oppLineItemClone.Quantity=10;
        insert oppLineItemClone;
        
        Test.startTest();
        CloneOpportunity.oppClone();
        Test.stopTest();
        
        List<Opportunity> oppNameList=[SELECT Id,Name FROM Opportunity WHERE Name LIKE '%Opportunity1%'];
        System.assertEquals('Opportunity1Clone',oppNameList[0].Name+'Clone');
    }
}

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi