+ Start a Discussion
Itayb34Itayb34 

Test Coverage - Copy Opportunity Products to Contract Line Items

Hello

 

I have the trigger below to copy opportunity M&S products to service contract when a service contract is created.

I have added the test coverage, but somehow I reach only 47% coverage, what am I missing? any help will be great!

 

Trigger:

trigger NewSCLineItems on ServiceContract (After Insert) {
    
    // Make a List of Opportunity ID's for all these quotes.
    Set<Id> oppIds = new Set<Id>();
    for(ServiceContract SC : Trigger.new) oppIds.add(SC.opportunity__c);
    
    // Fetch all the Opportunity Line Items for all these Opportunities
    List<OpportunityLineItem> olis = new List<OpportunityLineItem>([
        select 
             id, opportunityid, pricebookentryid, Quantity, UnitPrice, Discount
        from OpportunityLineItem 
        where opportunityid = :OppIds AND M_S_Product__c = 'TRUE'
    ]);
    

    // Build a Map, keyed by OppId, of Lists of the related OLI's
    Map<Id, List<OpportunityLineItem>> oliMap = new Map<Id, List<OpportunityLineItem>>();
    for (OpportunityLineItem oli: olis) {
       if (oliMap.containsKey(oli.OpportunityId)) {
            // If the map already has an entry for this Opp, add this OLI to the list.
            //oliMap.put(oli.OpportunityId, oliMap.get(oli.OpportunityId).add(oli));
            List<OpportunityLineItem> x;
            x = oliMap.get(oli.OpportunityId);
            x.add(oli);
            oliMap.put(oli.OpportunityId, x);
       } else {
            // This is the first entry for this Opportunity
            List<OpportunityLineItem> tmp = new List<OpportunityLineItem>();
            tmp.add(oli);
            oliMap.put(oli.OpportunityId, tmp);
       }
    }

   List<ContractLineItem> qli = new List<ContractLineItem>(); 
  
   // Iterate through each Contract
   for(ServiceContract sc : Trigger.new){
       // Do we have any OLI's for this Contract Opportunity?
       if (oliMap.containsKey(SC.opportunity__c)) {
           // Yes, so for each OLI in the List, create a QLI
           for (OpportunityLineItem oli: oliMap.get(SC.opportunity__c)) {
                qli.add(
                     New ContractLineItem (
                         ServiceContractId = SC.id,
                         pricebookentryid = oli.pricebookentryid,
                         UnitPrice = oli.UnitPrice,
                         Discount = oli.Discount,
                         Quantity = oli.Quantity
                     )
                );
           }
       }
    }
    if (! qli.isEmpty()) insert qli;
}

 

Test Class:

@IsTest(SeeAllData=true) private class CreateServiceContractLineItems{

    /* This is a basic test which simulates the primary positive case for the 
       save method in the quoteExt class. */

private static testMethod void myUnitTest() {

Account acc=new Account(Name='test');
insert acc;                

//Pricebook2 pb2=[Select id from Pricebook2 limit 1];

//Opportunity testOpp = new Opportunity(Name='TestOppty',closedate=date.parse('1/1/2222'),
//                    stagename = 'Qualification',AccountID=acc.id,Pricebook2Id=pb2.id);
//insert testOpp;

   Pricebook2 s = [select id from Pricebook2 where IsStandard = true];   
    
    // create the product
    Product2 p1 = new Product2(
        name='Test Product 1',
        IsActive=true,
        Description='My Product',
        ProductCode='12345'
    );
    insert p1;       
   
    // create the pricebookentry
    PricebookEntry pbe1 = new PricebookEntry(
        Pricebook2Id=s.id,
        Product2Id=p1.id,
        UnitPrice=0.00,
        IsActive=true,
        UseStandardPrice=false
    );
    insert pbe1;   
   
    // create the opportunity
    Opportunity opp1 = new Opportunity(
        name='Test Opp 1',
     ///   recordtypeid='01260000000DXrWxxx',
        StageName = 'Qualification',
        CloseDate = Date.newInstance(2009,01,01), 
        Pricebook2Id=s.id         
    );
    insert opp1;
    
   
    // add the line item
    OpportunityLineItem oli = new OpportunityLineItem();
    oli.Quantity = 1;
    oli.PricebookEntryId = pbe1.id;
    oli.OpportunityId = opp1.id;
    oli.UnitPrice=1;    
    insert oli;
    
    // add the 2nd line item
    OpportunityLineItem oli2 = new OpportunityLineItem();
    oli2.Quantity = 2;
    //oli2.TotalPrice = 2;
    oli2.PricebookEntryId = pbe1.id;
    oli2.OpportunityId = opp1.id;
    oli2.UnitPrice = 2;   
    insert oli2;
    
    ServiceContract sc =new ServiceContract(Name='test sc',AccountID=acc.id,Pricebook2Id=s.id, Opportunity__c=opp1.id );
    insert sc; 
    
    ContractLineItem cli = new ContractLineItem();
    cli.Quantity = oli.quantity;
    cli.PricebookEntryId = oli.PricebookEntryId;
    cli.ServiceContractId= sc.id;
    cli.UnitPrice= oli.UnitPrice;  
    insert cli;
    
    ContractLineItem cli2 = new ContractLineItem();
    cli2.Quantity = oli2.Quantity;
    cli2.PricebookEntryId = oli2.PricebookEntryId;
    cli2.ServiceContractId= sc.id;
    cli2.UnitPrice= oli2.UnitPrice;  
    insert cli2;
    
    System.assertEquals(cli2.UnitPrice, oli2.UnitPrice);

          }
}

 

Thanks in advance

 

Itay

Rakesh BoddepalliRakesh Boddepalli

In your class you are filtering the Opportunity records using the where clause "M_S_Product__c = 'TRUE'" . I don't see you are setting this value in the test class. Unless this has been automatically set using some workflow.

 

Thanks,

Rakesh B

Itayb34Itayb34

Added this filter (see below) and I got 78% coverage.

 

@IsTest (SeeAllData=true) private class CreateServiceContractLineItems{

    /* This is a basic test which simulates the primary positive case for the 
       save method in the quoteExt class. */

private static testMethod void myUnitTest() {

Account acc=new Account(Name='test');
insert acc;                

Pricebook2 s = [select id from Pricebook2 where IsStandard = true limit 1];   

     
    // create the product
    Product2 p1 = new Product2(
        name='M&S Product 1',
        IsActive=true,
        Description='My Product',
        ProductCode='12345'
        );
    insert p1;
    
    // create 2nd product
    Product2 p2 = new Product2(
        name='Base Product 2',
        IsActive=true,
        Description='My Base Product',
        ProductCode='11111'
        );
    insert p2;
         
   
    // create the pricebookentry
        PricebookEntry pbe1 = new PricebookEntry(
        Pricebook2Id=s.id,
        Product2Id=p1.id,
        UnitPrice=0.00,
        IsActive=true,
        UseStandardPrice=false
    );
    insert pbe1; 
    
     // create 2nd pricebookentry
        PricebookEntry pbe2 = new PricebookEntry(
        Pricebook2Id=s.id,
        Product2Id=p2.id,
        UnitPrice=0.00,
        IsActive=true,
        UseStandardPrice=false
    );
    insert pbe2;  
   
    // create the opportunity
        Opportunity opp1 = new Opportunity(
        name='Test Opp 1',
        StageName = 'Qualification',
        CloseDate = Date.newInstance(2009,01,01), 
        Pricebook2Id=s.id         
    );
    insert opp1;
    
   
    // add the line item
    OpportunityLineItem oli = new OpportunityLineItem(Quantity = 1,PricebookEntryId = pbe1.id, OpportunityId = opp1.id, UnitPrice=1);
   // OpportunityLineItem oli2 = new OpportunityLineItem(Quantity = 2,PricebookEntryId = pbe2.id, OpportunityId = opp1.id, UnitPrice=2);
    
    // add 2nd line item
    OpportunityLineItem oli2 = new OpportunityLineItem();
    oli2.Quantity = 2;
    oli2.PricebookEntryId = pbe2.id;
    oli2.OpportunityId = opp1.id;
    oli2.UnitPrice = 2;  
    insert oli2;
      
    List<OpportunityLineItem> oppline= new List<OpportunityLineItem>();
    oppline.add(oli);
    //oppline.add(oli2);
    insert oppline; 
    
    oli =  [SELECT Id, M_S_Product__c,Quantity,UnitPrice,PricebookEntryId
             FROM OpportunityLineItem
             WHERE Id = :oli.Id];
    
    oli2 =  [SELECT Id, M_S_Product__c,Quantity,UnitPrice,PricebookEntryId
             FROM OpportunityLineItem
             WHERE Id = :oli2.Id];
    
    System.assertEquals('TRUE', oli.M_S_Product__c); 
    System.assertEquals('FALSE', oli2.M_S_Product__c);
    
    ServiceContract sc =new ServiceContract(Name='test sc',AccountID=acc.id,Pricebook2Id=s.id, Opportunity__c=opp1.id );
    insert sc; 
    
    ContractLineItem cli = new ContractLineItem();
    cli.Quantity = oli.quantity;
    cli.PricebookEntryId = oli.PricebookEntryId;
    cli.ServiceContractId= sc.id;
    cli.UnitPrice= oli.UnitPrice;  
    insert cli;
    
    cli =  [SELECT Id,Quantity,UnitPrice,PricebookEntryId
             FROM ContractLineItem
             WHERE Id = :cli.Id];
    
    System.assertEquals(1, cli.UnitPrice);
    
    ContractLineItem cli2 = new ContractLineItem();
    cli2.Quantity = oli2.Quantity;
    cli2.PricebookEntryId = oli2.PricebookEntryId;
    cli2.ServiceContractId= sc.id;
    cli2.UnitPrice= oli2.UnitPrice;  
    insert cli2;
    
    cli2 =  [SELECT Id,Quantity,UnitPrice,PricebookEntryId
             FROM ContractLineItem
             WHERE Id = :cli2.Id];
             
    System.assertEquals(2, cli2.UnitPrice);
    

          }
}

 

This part in the trigger is not covered, any idea how can I cover it?

 

List<OpportunityLineItem> x;
x = oliMap.get(oli.OpportunityId);
x.add(oli);
oliMap.put(oli.OpportunityId, x);

 

 

 

Rakesh BoddepalliRakesh Boddepalli

Change the Opportunity creation in the test class as below .. check my comments in there

  // create the opportunity
        Opportunity opp1 = new Opportunity(
        name='Test Opp 1',
        StageName = 'Qualification',
        CloseDate = Date.newInstance(2009,01,01), 
// Rakesh - Added the below line (remove the single quotes if it is a boolean field
M_S_Product__c = 'TRUE'; Pricebook2Id=s.id ); insert opp1;
Itayb34Itayb34

"M_S_Product__c" is a field on opportunity products (not opportunities), so I can't add it on opportunity creation