+ Start a Discussion
LightsLights 

Apex Trigger: Increasing coverage. Help?

Hello Friends,

I'm getting the following error because the trigger doesn't have enough coverage. How do I accomplish that? Thank you!
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
 
trigger Product00001 on OpportunityLineItem( after insert, after update )
{
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    Set<Id> opptyIds = new Set<Id>();
 
    for( OpportunityLineItem optLineItem: trigger.new )
    {
        if( optLineItem.ProductCode == '00001a' )
        {
            opptyIds.add( optLineItem.OpportunityId );
        }
    }
 
    if( opptyIds.size() > 0 )
    {
        //retrieve the values based on Product list
List<OpportunityLineItem> lstOpptyLineItems = [ SELECT Opportunity.Pricebook2Id, OpportunityId, Name, ProductCode,
                                                        PricebookEntryId, Quantity, UnitPrice
                                                        FROM OpportunityLineItem
                                                        WHERE OpportunityId IN: opptyIds order by ProductCode ];
 
        Map<Id, List<OpportunityLineItem>> mapOpptyLineItem = new Map<Id, List<OpportunityLineItem>>();
        for( OpportunityLineItem item : lstOpptyLineItems )
        {
            List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>();
            if( mapOpptyLineItem.containsKey( item.OpportunityId ))
            {
                oliList1 = mapOpptyLineItem.get( item.OpportunityId );
            }
            
            oliList1.add( item );
            mapOpptyLineItem.put( item.OpportunityId, oliList1 );
        }
 
 
        //retrieve PriceBookEntry of the Product B, this is most important
        PricebookEntry pbeProduct2 = [ SELECT Id, Pricebook2Id, UnitPrice, Name, Percentage_Entry__c
                                        FROM PricebookEntry
                                        WHERE Name ='Product 00001'
                                        AND Pricebook2Id  IN (SELECT Id FROM PriceBook2 WHERE Id ='01sA00000004lbRIAQ')
                                        LIMIT 1 ];
 
         if( trigger.isInsert )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001a' )
                     {
                         oliList.add( new OpportunityLineItem(
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = item.Quantity,
                                            UnitPrice = item.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 insert oliList;
         }
         else if( trigger.isUpdate )
         {
             for( Id oppId : mapOpptyLineItem.keySet() )
             {
                 OpportunityLineItem itemProductA = new OpportunityLineItem();
                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001a' )
                     {
                         itemProductA = item;
                         break;
                     }
                 }

                 for( OpportunityLineItem item : mapOpptyLineItem.get( oppId ))
                 {
                     if( item.ProductCode == '00001b' )
                     {
                         oliList.add( new OpportunityLineItem( Id = item.Id,
                                            OpportunityId = oppId,
                                            PricebookEntryId = pbeProduct2.Id,
                                            Quantity = itemProductA.Quantity,
                                            UnitPrice = itemProductA.UnitPrice * pbeProduct2.Percentage_Entry__c * 0.01 )
                                          );
                     }
                 }
             }
             
             if( oliList.size() > 0 )
                 update oliList;
         }
    }
}

 
Leonardi KohLeonardi Koh
Hi Lights,

You will have to write a test class to provide coverage for your trigger, this is standard with Salesforce APEX development.
Test class in Salesforce effectively allow us to simulate scenarios that the tested logic and functions would normally do, without actually persisting the action into the system (so it's safe as far as the database is concerned) and whatever lines of your code were executed in the process by the test class when it ran will be considered as covered.

In this case i can see you have a trigger on OpportunityLineItem with after insert and update, so your test class will have to simulate this process by generating and/or updating a test record of OpportunityLineItem.

For more information on test classes please see https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro
LightsLights

I actually have a test class. Sorry. I didn't post it.

 

@isTest( seeAllData = true )
private class OpportunityLineItemTrigger_Test
{
    static testMethod void test_OpportunityLineItemTrigger()
    {
        Account acc = new Account( Name = 'Test' );
        insert acc;
        
        Pricebook2 p = new Pricebook2(Name = 'Testbook');
        insert p;
               
        Opportunity opp = new Opportunity(
        Name = 'Test',
        AccountId = acc.Id,
        StageName = 'New',
        CloseDate = Date.today() );
        insert opp;
        
        Pricebook2 standard = [
        Select Id,
        Name,
        IsActive
        From Pricebook2 Limit 1 ];
        
        Product2 prod =new Product2(
        Name='Product 00001',
        ProductCode='Product00001a',
        isActive = true );
        insert prod;
        
        PricebookEntry pbe = new PricebookEntry(
        pricebook2Id = Test.getStandardPricebookId(),
        product2id = prod.id,
        unitprice = 1249.0,
        isactive = true);
        insert pbe;
        
        OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(
        OpportunityId = opp.id,
        PricebookEntryId = pbe.id,
        Quantity = 9,
        UnitPrice = 13);
        test.startTest();
        insert opportunitylineitem_Obj;
        test.stopTest();
    }

    static testMethod void test_UseCase2()
    {
        Account acc = new Account( Name = 'Test' );
        insert acc;
        
        Pricebook2 p = new Pricebook2(Name = 'Testbook');
        insert p;
        
        Opportunity opp = new Opportunity(
        Name = 'Test',
        AccountId = acc.Id,
        StageName = 'New',
        CloseDate = Date.today());
        insert opp;
        
        Pricebook2 standard = [
        Select Id,
        Name,
        IsActive
        From Pricebook2 LIMIT 1 ];
        
        Product2 prod =new Product2(
        Name='Product 00001',
        ProductCode='Product00001a',
        isActive = true );
        insert prod;
        
        PricebookEntry pbe = new PricebookEntry(
        pricebook2Id = Test.getStandardPricebookId(),
        product2id = prod.id,
        unitprice = 1249.0,
        isactive = true );
        insert pbe;
        
        OpportunityLineItem opportunitylineitem_Obj = new OpportunityLineItem(
        OpportunityId = opp.id,
        PricebookEntryId = pbe.id,
        Quantity = 9,
        UnitPrice = 13);
        test.startTest();
            insert opportunitylineitem_Obj;
            
            opportunitylineitem_Obj.Quantity = 10;
            update opportunitylineitem_Obj;
        test.stopTest();
    }
}
 

 

Leonardi KohLeonardi Koh
Did the test classes ran successfully without any error?
LightsLights
it did... confusing right :(
Leonardi KohLeonardi Koh
The trigger is active pressumably? If so, it might be a good idea to check the debug log for the test class and see if the OppLineItem trigger ran at all.