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
SSebSSeb 

Please help with the code coverage for the trigger

trigger Product_Schedule on OpportunityLineItem (after update,after insert) 
{
    for ( OpportunityLineItem o : Trigger.new)
    { 
        Opportunity opps;
        // id OppLineItemID = o.Opp_Line_Item_Id__c ;
        id OppLineItemID = o.ID ;
        system.debug('Opurtunity line item triggered ' + o.Opp_Line_Item_Id__c  + 'Opportunity ID ' + o.ID);
        
        opps =  [Select  o.First_Ship_Date__c  ,o.Expected_Return_Ship_Date__c, o.Min_K_QPID__c,
                 (Select Quantity, productcode  From OpportunityLineItems where ProductCode = 'K-QPID') From Opportunity o 
                 Where id IN (SELECT OpportunityId FROM OpportunityLineItem WHERE Quantity > 0 and id =:o.id) ];
        
        OpportunityLineItem prod;
        prod =  [Select Quantity, productcode, Nav_Inventory__c  From OpportunityLineItem where Opp_Line_Item_Id__c =:o.Opp_Line_Item_Id__c];
        
        Datascan_Product_Availability__c Avail = [select K_QPID__c , Count_Date__c From Datascan_Product_Availability__c c 
                                                  where (c.Count_Date__c > :opps.First_Ship_Date__c) and (c.Count_Date__c < :opps.Expected_Return_Ship_Date__c) 
                                                  order by Count_Date__c asc limit 1 ];
        
        opps.Min_K_QPID__c  = Avail.K_QPID__c ;
        opps.Min_K_QPID_Date__c = Avail.Count_Date__c;
        update opps;
        
        //select MIN(K_DS_WIFI_1__c) From Datascan_Product_Availability__c where count_date__C > 2019-10-18 and count_date__c < 2019-11-22
        system.debug('opps ' + opps.First_Ship_Date__c + opps.Expected_Return_Ship_Date__c );
        system.debug('prod ' + prod.quantity + prod.productcode );
        system.debug('Avail ' + Avail.K_QPID__c  );
        
    }
}
PINKY REGHUPINKY REGHU
hi,
 Try this
@isTest
public class Product_ScheduleTest {
    static testMethod void Scheduletest()
    {
        Account a= new Account(name='test account');
        insert a;
        Opportunity opp= new Opportunity(name='test opp',accountId=a.Id, First_Ship_Date__c=date.today(),Expected_Return_Ship_Date__c=date.today()+5,Min_K_QPID__c='XXXX');
        insert opp;
        product2 product= new product2( Name = 'Product X',ProductCode = 'K-QPID', isActive = true);
        insert product;
        Id pricebook_Id=System.Test.getStandardPricebookId();
        pricebookEntry price_entry= new pricebookEntry(  Pricebook2Id = pricebook_Id, Product2Id = product.Id, UnitPrice = 100.00,IsActive = true);
        insert price_entry;
        OpportunitylineItem opplineitem = new OpportunitylineItem(OpportunityId = opp.Id, Quantity = 5, PricebookEntryId = price_entry.Id,TotalPrice = quantity * pbEntry.UnitPrice, Nav_Inventory__c='xxxx');
        insert opplineitem;
        Datascan_Product_Availability__c Availtest= new  Datascan_Product_Availability__c(K_QPID__c='xxxx' , Count_Date__c =data.today()+3);
        insert Availtest;
    }
}

 
Ajay K DubediAjay K Dubedi
Hi,

Here is your test class for trigger-
 
@isTest
public class Product_Schedule_test {
    static testMethod void test()
    {
        Account acc = new Account(name='test account');
        insert acc;

        Opportunity opp= new Opportunity(name='test opp',accountId=acc.Id, First_Ship_Date__c=date.today(),Expected_Return_Ship_Date__c=date.today()+5,Min_K_QPID__c='XXXX');
        insert opp;

        product2 product= new product2( Name = 'Product test',ProductCode = 'K-QPID', isActive = true);
        insert product;

        Id pricebook_Id=System.Test.getStandardPricebookId();
        pricebookEntry price_entry= new pricebookEntry(  Pricebook2Id = pricebook_Id, Product2Id = product.Id, UnitPrice = 100.00,IsActive = true);
        insert price_entry;

  Datascan_Product_Availability__c Availtest= new  Datascan_Product_Availability__c(K_QPID__c='xxxx' , Count_Date__c =data.today()+3);
        insert Availtest;

        OpportunitylineItem opplineitem = new OpportunitylineItem(OpportunityId = opp.Id, Quantity = 10, PricebookEntryId = price_entry.Id,TotalPrice = 1000.00, Nav_Inventory__c='xxxx');
        insert opplineitem;

       
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com  (http://www.ajaydubedi.com )
SSebSSeb
Thanks for your help Ajay. But the test method failed.
SSebSSeb
Worked on the code and I was able to fix errors except this. Any help is appreciated.

@isTest
public class Product_Schedule_Helper
{
    static testMethod void Scheduletest()
    {
        Account a= new Account(name='test account');
        insert a;
        
        Opportunity opp= new Opportunity(name='test opp',accountId=a.Id, StageName = 'Estimated Details', CloseDate =date.today()+5,SR_AE_Win_Probability__c = '25%',
                                         SR_AE_Forecast_Category__c = 'Pipeline',
                                         First_Ship_Date__c=date.today()-5,Expected_Return_Ship_Date__c=date.today()+5,Min_K_QPID__c=222);
        insert opp;
        
        product2 product= new product2(Name = 'K-DART SCANNER',ProductCode = 'K-QPID', isActive = true);
        insert product;
        
        Id pricebook_Id=System.Test.getStandardPricebookId();
        pricebookEntry price_entry= new pricebookEntry(  Pricebook2Id = pricebook_Id, Product2Id = product.Id, UnitPrice = 0.02,IsActive = true);
        insert price_entry;
        
        OpportunitylineItem opplineitem = new OpportunitylineItem (OpportunityId = opp.Id,Quantity=110, UnitPrice = 0.02);
        insert opplineitem;
        
        Datascan_Product_Availability__c Availtest= new  Datascan_Product_Availability__c (K_QPID__c=233 ,Count_Date__c = date.today()+3);
        
        insert Availtest;
    }
}
SSebSSeb
Error -
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown]
PINKY REGHUPINKY REGHU
Hi,
Make sure while creating OpportunitLineItem (Opportunity Product), PricebookEntryId is not null.Hope this helps to solve  dml exception.
SSebSSeb
Hi thank you both for your help. Still strugling with this - I get 75% code coverage in Sandbox but when I push it to Production I get 0% code coverage. What am I doing wrong? Here is the final code. I deployed both my trigger and helper class.

@isTest
public class Product_Schedule_Helper
{
    static testMethod void Scheduletest()
    {
        Account a= new Account(name='test account');
        insert a;
        
        Opportunity opp= new Opportunity(name='test opp',accountId=a.Id, StageName = 'Estimated Details', CloseDate =date.today()+5,SR_AE_Win_Probability__c = '25%',
                                         SR_AE_Forecast_Category__c = 'Pipeline',
                                         First_Ship_Date__c=date.today()-5,Expected_Return_Ship_Date__c=date.today()+5,Min_K_QPID__c=222);
        insert opp;
        
        product2 product= new product2(Name = 'K-DART SCANNER',ProductCode = 'K-QPID', isActive = true);
        insert product;
        
        Id pricebook_Id=System.Test.getStandardPricebookId();
        pricebookEntry price_entry= new pricebookEntry(  Pricebook2Id = pricebook_Id, Product2Id = product.Id, UnitPrice = 0.02,IsActive = true);
        insert price_entry;
        
        
        Datascan_Product_Availability__c Availtest= new  Datascan_Product_Availability__c (K_QPID__c=233 ,Count_Date__c = date.today()+3);
        insert Availtest;
       
        OpportunitylineItem opplineitem = new OpportunitylineItem(OpportunityId = opp.Id, Quantity = 10, PricebookEntryId = price_entry.Id,TotalPrice = 1000.00);
        insert opplineitem;
       

    }
}