+ Start a Discussion
HTANIRSHTANIRS 

trigger test class code coverage issue

Hi,

Kindly help me in reaching 75% in code coverage. I am getting 45% for the below trigger. Kindly refer the Trigger and Test Class and let me know what change to achieve 75%.
 
Trigger -----

trigger insertOppsLineitem on OrderItems__c (after insert) {
    System.debug('--- Inside OrderItems Trigger ---');      
          
    Set<Decimal> orderIds = new Set<Decimal>();
    
    for(OrderItems__c oi : Trigger.New) {
        orderIds.add(oi.OrderId__c);
        System.debug('--- Getting Custom Obj Id: ---' + orderIds);
    }
    
    Map<Decimal, List<Opportunity>> oppMap = new Map<Decimal, List<Opportunity>>();
    
    if(!orderIds.isEmpty()){
        System.debug('--- Checking Order ID: ---');
        for(Opportunity opp : [SELECT Id, Name, OrderItem__c FROM Opportunity WHERE OrderItem__c IN: orderIds]) {
            if(!oppMap.containsKey(opp.OrderItem__c)) {
                oppMap.put(opp.OrderItem__c, new List<Opportunity> {opp});
                System.debug('--- Opportunity Map ---' + oppMap);
            }            
        } 
    }
    
    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];

    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    
    for(OrderItems__c oi : Trigger.New){
        System.debug('--- Order Items: ---' + oi);
        if(oppMap.containsKey(oi.OrderId__c)){
            for(Opportunity opp : oppMap.get(oi.OrderId__c)) {
                System.debug('--- Inside Opportunity ---');
                OpportunityLineItem oli = new OpportunityLineItem();                
                oli.OpportunityId = opp.Id;
                oli.PricebookEntryId = priceBookList[0].Id; 
                oli.Quantity = oi.Qty_Ordered__c;
                oli.TotalPrice = oi.Price__c;                
                oli.item_id__c = oi.Name;
                oli.Name__c = oi.Name__c;
                oliList.add(oli);
                System.debug('--- Inside Opportunity' + opp);
                System.debug('--- Oli List: ---');
            }
        }
    }
    try {
        if(oliList.size()>0) {
            insert oliList;
            System.debug('--- Inserted Opp Line Items: ---');
            System.debug('--- Inserted Opp Line Items: ---' + olilist.size()); 
        }
    }
    catch(Exception e){
        System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
    }
}
 
Test Class ----

@isTest
private class insertOppsLineitemTest {
    static testMethod void testorderItems() {
            OrderItems__c oi = new OrderItems__c();
            oi.Qty_Ordered__c = 1;
            oi.OrderId__c = 2333;
            oi.Price__c = 100;                
            oi.Name = 'Test';            
            insert oi;
            
            OrderItems__c o = [SELECT Id, Name FROM OrderItems__c WHERE Name = 'Test'];            
            
            Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('AccountRecordType').getRecordTypeId();
            
            Account acc = new Account();
            acc.Name = 'Test';
            acc.recordtypeid = RecordTypeIdAccount;             
            insert acc; 
            
            Opportunity opp = new Opportunity();
            opp.Name = 'Test';
            opp.CloseDate = system.today();
            opp.StageName = 'New';
            opp.Account.Id = acc.Id;
            opp.OrderItem__c = oi.OrderId__c;
            insert opp;
                        
            List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity WHERE Name = 'Test'];
            
            List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];
            
            Id pricebookId = Test.getStandardPricebookId();
            
            //Create your product
            Product2 prod = new Product2(
                 Name = 'Product X',
                 ProductCode = 'Pro-X',
                 isActive = true
            );
            insert prod;
            
            //Create your pricebook entry
            PricebookEntry pbEntry = new PricebookEntry(
                 Pricebook2Id = pricebookId,
                 Product2Id = prod.Id,
                 UnitPrice = 100.00,
                 IsActive = true
            );
            insert pbEntry;
            
            OpportunityLineItem oli = new OpportunityLineItem();                
            oli.OpportunityId = opp.Id;
            oli.PricebookEntryId = pbEntry.Id; 
            oli.Quantity = 1;
            oli.TotalPrice = 100;                
            oli.item_id__c = '1254';
            oli.Name__c = 'Testing';
            insert oli;            
            
    }
}

 
Raj VakatiRaj Vakati
What lines are not covering .. ?
HTANIRSHTANIRS
Hi Raj

Kindly check the below updated Trigger and Test Class. I am getting 58% code coverage.
Insert Method is not covering.
Trigger::

trigger insertOppsLineitem on OrderItems__c (after insert) {    
    Set<String> orderitemsSet = new Set<String>();
    Set<String> orderitemsDelSet = new Set<String>();
    List<Opportunitylineitem> oliList = new List<Opportunitylineitem>();
    List<Opportunitylineitem> oliinsertList = new List<Opportunitylineitem>();
    List<Opportunitylineitem> oliupdateList = new List<Opportunitylineitem>();
    List<OrderItems__c> orderitemsListtoDelete = new List<OrderItems__c>();
    
    for(OrderItems__c oi : Trigger.new) {
        orderitemsSet.add(oi.Name);
        orderitemsSet.add(oi.Product_Id__c);
    }
    
    Map<String, List<Opportunity>> oppMap = new Map<String, List<Opportunity>>();
    
    List<Opportunity> oppList = new List<Opportunity>();
    oppList = [SELECT Id, Name FROM Opportunity WHERE Name IN: orderitemsSet];
    
    Map<String, List<pricebookentry>> pricebookMap = new Map<String, List<pricebookentry>>();
    
    List<Pricebookentry> pbeList = new List<Pricebookentry>();
    pbeList = [SELECT Id, Name, Pricebook2Id, Pricebook2.Name, Product2Id, IsActive, Product2.entity_id__c
                  FROM Pricebookentry 
                  WHERE IsActive = true AND Pricebook2.Name = 'Standard Price Book' AND Product2.entity_id__c IN: orderitemsSet];
                  
    Pricebookentry pbe = [SELECT Id, Name, isActive FROM Pricebookentry WHERE isActive = true AND Name = 'Test Product' Limit 1];
    
    if(!orderitemsSet.isEmpty()) {
        for(Opportunity opps : oppList){
            if(!oppMap.containsKey(opps.Name)){
                oppMap.put(opps.Name, new List<Opportunity> {opps});
                System.debug('--- Inside OppMap ---' + oppMap);
            }
            else{
                List<Opportunity> oppList = oppMap.get(opps.Name);
                oppList.add(opps);
                oppMap.put(opps.Name, oppList);
                System.debug('--- Else oppMap ---' + oppList);
            }
        }
        
        for(Pricebookentry pbe : pbeList) {
            if(!pricebookMap.containsKey(pbe.Product2.entity_id__c)) {
                pricebookMap.put(pbe.Product2.entity_id__c, new List<Pricebookentry> {pbe});
            }
            else {
                List<Pricebookentry> pbeList = pricebookMap.get(pbe.Product2.entity_id__c);
                pbeList.add(pbe);
                pricebookMap.put(pbe.Product2.entity_id__c, pbeList);
            }
        }
    }
    
    Set<Decimal> oliIds = new Set<Decimal>();
    Map<Decimal, List<Opportunitylineitem>> oppliMap = new Map<Decimal, List<Opportunitylineitem>>();
    
    for(OrderItems__c oi : Trigger.New) {
        oliIds.add(oi.item_id__c );
        System.debug('--- Order Ids ---' + oi.item_id__c);
    }
    
    List<Opportunitylineitem> oppliList = new List<Opportunitylineitem>();
    oppliList = [SELECT Id, Item_id__c FROM Opportunitylineitem WHERE Item_id__c IN: oliIds];
    
    if(!oliIds.isEmpty()) {
        for(Opportunitylineitem oplis : oppliList){
            if(!oppliMap.containsKey(oplis.item_id__c)){
                oppliMap.put(oplis.item_id__c, new List<Opportunitylineitem> {oplis});
                System.debug('--- Inside OppMap ---' + oppliMap);
            }
            else{
                List<Opportunitylineitem> opliList = oppliMap.get(oplis.item_id__c);
                opliList.add(oplis);
                oppliMap.put(oplis.item_id__c, opliList);
                System.debug('--- Else oppMap ---' + opliList);
            }
        }
    }
    
    for(OrderItems__c oi : Trigger.new) {
        if(oppMap.containsKey(oi.Name)) {
            //Inserting into Opportunity with Opportunity Name.
            for(Opportunity opp : oppMap.get(oi.Name)) {
                 // Insert Method
                 //Checking if Opportunitylineitem does not exists in Opportunity.
                 if(!oppliMap.containsKey(Oi.item_Id__c)) {
                     if(pricebookMap.containsKey(oi.Product_Id__c)) {
                
                        //Getting the Pricebookentry Name and Inserting Opportunity Lineitem.
                        for(Pricebookentry pe : pricebookMap.get(oi.Product_Id__c)) {
                            Opportunitylineitem oli = new Opportunitylineitem();
                            oli.OpportunityId = opp.Id;
                            oli.PricebookEntryId = pe.Id;
                            oli.Quantity = 1;
                            oli.TotalPrice = oi.Price__c;                
                            oli.item_id__c = oi.item_id__c;
                            oli.Name__c = oi.Name__c;
                            oli.product_id__c = oi.product_id__c;
                            oli.Due_Date__c = oi.Due_Date__c;
                            oli.product_type__c = oi.product_type__c;
                            oliinsertList.add(oli);
                            orderitemsDelSet.add(oi.Id);
                        }
                    }                                                    
                    else {
                        Opportunitylineitem oli = new Opportunitylineitem ();
                        oli.OpportunityId = opp.Id;
                        oli.PricebookEntryId = pbe.Id;
                        oli.Quantity = 1;
                        oli.TotalPrice = oi.Price__c;                
                        oli.item_id__c = oi.item_id__c;
                        oli.Name__c = oi.Name__c;
                        oli.product_id__c = oi.product_id__c;
                        oli.Due_Date__c = oi.Due_Date__c;
                        oli.product_type__c = oi.product_type__c;
                        oliinsertList.add(oli);
                        orderitemsDelSet.add(oi.Id);
                    }
                }
                //update method
                else {
                    for(Opportunitylineitem oli : oppliList) {                   
                        Opportunitylineitem olits = new Opportunitylineitem(Id = oli.Id);
                        olits.Quantity = 1;
                        olits.TotalPrice = oi.Price__c;
                        olits.item_id__c = oi.item_id__c;
                        olits.Name__c = oi.Name__c;
                        olits.product_id__c = oi.product_id__c;
                        olits.Due_Date__c = oi.Due_Date__c;
                        olits.product_type__c = oi.product_type__c;
                        oliupdateList.add(olits);
                        orderitemsDelSet.add(oi.Id);
                    }
                }
            }
        }
    }
    
    if(orderitemsDelSet.size() > 0) {
        orderitemsListtoDelete = [Select Id from OrderItems__c where Id IN: orderitemsDelSet];
    }
    
    //Insert, Update and Delete Operations.
    try {        
        if(oliinsertList.size() > 0) {    
            insert oliinsertList;
        }
        if(oliupdateList.size() > 0) {
            update oliupdateList;
        }
        if(orderitemsListtoDelete.size() > 0) {
            delete orderitemsListtoDelete; 
        }                
    }
    catch(Exception e) {
        System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
    }
}
 
Test Class:

@isTest(SeeAllData=true)
public class insertOppsLineitemTest {
    static testMethod void testOrderitems() {
        
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
  
        Account acc = new Account(LastName = 'Test Account', recordtypeid = RecordTypeIdAccount);
        insert acc;
        
        Opportunity opps = new Opportunity(Name = '007', CloseDate = system.today(), StageName = 'New', AccountId = acc.Id);
        insert opps;
        
        product2 prod = new Product2(Name = 'test', entity_id__c = '12454');
        insert prod;
        
        Pricebookentry pbey = new Pricebookentry(isActive = true, product2Id = prod.Id, UnitPrice=50, Pricebook2ID=standardPb.id);
        insert pbey;
        
        OrderItems__c oi = new OrderItems__c();
        oi.Qty_Ordered__c = 1;
        oi.Name = '007';
        oi.Price__c = 100;                
        oi.Item_Id__c = 123;
        oi.Name__c = 'test';
        oi.product_id__c = '12454';
        oi.Due_Date__c = system.today();
        oi.product_type__c = '1254';
        insert oi;
    }               
}

Thanks.
Raj VakatiRaj Vakati
Can u give me which lines are not covering with screenshot 
HTANIRSHTANIRS
Hi Raj,

Kindly refer the below screenshot.

User-added image

Thanks.
Raj VakatiRaj Vakati
try this
 
@isTest(SeeAllData=true)
public class insertOppsLineitemTest {
    static testMethod void testOrderitems() {
        
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
  
        Account acc = new Account(LastName = 'Test Account', recordtypeid = RecordTypeIdAccount);
        insert acc;
        
        Opportunity opps = new Opportunity(Name = '007', CloseDate = system.today(), StageName = 'New', AccountId = acc.Id);
        insert opps;
        
        product2 prod = new Product2(Name = 'test', entity_id__c = '12454');
        insert prod;
        
        Pricebookentry pbey = new Pricebookentry(isActive = true, product2Id = prod.Id, UnitPrice=50, Pricebook2ID=standardPb.id);
        insert pbey;
        
		
		 OpportunityLineItem olines = new OpportunityLineItem (Quantity=25, OpportunityId=opps.Id,UnitPrice=1,PriceBookEntryId=pbe2.Id
                                     );
		insert olines;
 
 
        OrderItems__c oi = new OrderItems__c();
        oi.Qty_Ordered__c = 1;
        oi.Name = '007';
        oi.Price__c = 100;                
        oi.Item_Id__c = 123;
		oi.item_Id__c =olines.Id ; 
        oi.Name__c = 'test';
        oi.product_id__c = '12454';
        oi.Due_Date__c = system.today();
        oi.product_type__c = '1254';
        insert oi;
		
		
        OrderItems__c oi1 = new OrderItems__c();
        oi1.Qty_Ordered__c = 1;
        oi1.Name = '007';
        oi1.Price__c = 100;                
        oi1.Item_Id__c = 123;
		oi1.item_Id__c =olines.Id ; 
        oi1.Name__c = 'test';
        oi1.product_id__c = '12454';
        oi1.Due_Date__c = system.today();
        oi1.product_type__c = '1254';
        insert oi1;
		
		
		 OrderItems__c oi11 = new OrderItems__c();
        oi11.Qty_Ordered__c = 1;
        oi11.Name = '007';
        oi11.Price__c = 100;                
        oi11.Item_Id__c = 123;
        oi11.Name__c = 'test';
        oi11.product_id__c = '12123454';
        oi11.Due_Date__c = system.today();
        oi11.product_type__c = '1212354';
        insert oi11;
    }               
}