+ Start a Discussion
HTANIRSHTANIRS 

trigger test class coverage for opportunity lineitem

Hello Friends,

Need help in trigger test class code coverage. I am getting 53% code coverage.

Requirement: Checking Opportunity Line Item already exists in Opportunity. If exists display error.

Kindly check the code below and let me know what change need to be done.
Trigger:

trigger avoiddupOli on OpportunityLineItem (before insert) {
    System.debug('---Inside Oli Trigger---');
    Map<Id,List<Id>> mapProduct = new Map<Id,List<Id>>();
    Set<Id> setOppIds = new Set<Id>();
    for(OpportunityLineItem oli: Trigger.new){
        setOppIds.add(oli.OpportunityId);
        System.debug('--- Opportunity Id ---' + oli.OpportunityId);
    }
    for(OpportunityLineItem oli: [Select Id, Product2Id, OpportunityId from OpportunityLineItem where OpportunityId IN:setOppIds]){
        if(mapProduct.containsKey(oli.OpportunityId))
            mapProduct.get(oli.OpportunityId).add(oli.Product2Id);
        else
            mapProduct.put(oli.OpportunityId,new List<Id>{oli.Product2Id});
    }
    for(OpportunityLineItem oli: Trigger.new){
        if(mapProduct.containsKey(oli.OpportunityId)){
            for(Id prodId: mapProduct.get(oli.OpportunityId)){
                if(oli.Product2Id.equals(prodId))
                    oli.addError('Product already Exists');
                    System.debug('--- Product Error ---' + oli.Product2Id);
            }
        }
    }
}
 
Test Class:

@isTest(SeeAllData=true)
public class avoiddupOliTest {
    static testMethod void testUnit() {
        
        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 = 'Account Test', recordtypeid = RecordTypeIdAccount);
        insert acc;
        
        Opportunity opps = new Opportunity(Name = 'Test Opps', CloseDate = system.today(), StageName = 'New', AccountId = acc.Id);
        insert opps;
        
        product2 prod = new Product2(Name = 'Test Prod', entity_id__c = '98765');
        insert prod;
        
        Pricebookentry pbey = new Pricebookentry(isActive = true, product2Id = prod.Id, UnitPrice = 50, Pricebook2ID = standardPb.id);
        insert pbey;
        
        Test.startTest();
        
            OpportunityLineItem item = new OpportunityLineItem(
                pricebookentryid = pbey.Id,
                Name__c = 'Test Prod',
                product_id__c = '98765',
                TotalPrice = 100,
                Quantity = 1,
                OpportunityID = opps.Id
            );
            
            try { 
                insert item;
            }
            catch (Exception duplicate) {
            
            }
            
        Test.stopTest();
    }
}

Thanks.
Best Answer chosen by HTANIRS
Raj VakatiRaj Vakati
try this
 
Test Class:

@isTest(SeeAllData=false)
public class avoiddupOliTest {
    static testMethod void testUnit() {
        
        
        
        Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
  
        Account acc = new Account(LastName = 'Account Test', recordtypeid = RecordTypeIdAccount);
        insert acc;
        
        Opportunity opps = new Opportunity(Name = 'Test Opps', CloseDate = system.today(), StageName = 'New', AccountId = acc.Id);
        insert opps;
        
		
		 
       
		 // First, set up test price book entries.
        // Insert a test product.
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Hardware');
        insert prod;
        
        // Get standard price book ID.
        // This is available irrespective of the state of SeeAllData.
        Id pricebookId = Test.getStandardPricebookId();
        
        // 1. Insert a price book entry for the standard price book.
        // Standard price book entries require the standard price book ID we got earlier.
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Test.startTest();
        
            
          
            
            try { 
			OpportunityLineItem item = new OpportunityLineItem(
                pricebookentryid = standardPrice.Id,
                Name__c = 'Test Prod',
                product_id__c = '98765',
                TotalPrice = 100,
                Quantity = 1,
                OpportunityID = opps.Id  );
                insert item;
				
				
				 OpportunityLineItem item1 = new OpportunityLineItem(
                pricebookentryid = standardPrice.Id,
                Name__c = 'Test Prod',
                product_id__c = '98765',
                TotalPrice = 100,
                Quantity = 1,
                OpportunityID = opps.Id  );
				insert item1 ;
            );
				
            }
            catch (Exception duplicate) {
            
            }
            
        Test.stopTest();
    }
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
Test Class:

@isTest(SeeAllData=false)
public class avoiddupOliTest {
    static testMethod void testUnit() {
        
        
        
        Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
  
        Account acc = new Account(LastName = 'Account Test', recordtypeid = RecordTypeIdAccount);
        insert acc;
        
        Opportunity opps = new Opportunity(Name = 'Test Opps', CloseDate = system.today(), StageName = 'New', AccountId = acc.Id);
        insert opps;
        
		
		 
       
		 // First, set up test price book entries.
        // Insert a test product.
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Hardware');
        insert prod;
        
        // Get standard price book ID.
        // This is available irrespective of the state of SeeAllData.
        Id pricebookId = Test.getStandardPricebookId();
        
        // 1. Insert a price book entry for the standard price book.
        // Standard price book entries require the standard price book ID we got earlier.
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Test.startTest();
        
            
          
            
            try { 
			OpportunityLineItem item = new OpportunityLineItem(
                pricebookentryid = standardPrice.Id,
                Name__c = 'Test Prod',
                product_id__c = '98765',
                TotalPrice = 100,
                Quantity = 1,
                OpportunityID = opps.Id  );
                insert item;
				
				
				 OpportunityLineItem item1 = new OpportunityLineItem(
                pricebookentryid = standardPrice.Id,
                Name__c = 'Test Prod',
                product_id__c = '98765',
                TotalPrice = 100,
                Quantity = 1,
                OpportunityID = opps.Id  );
				insert item1 ;
            );
				
            }
            catch (Exception duplicate) {
            
            }
            
        Test.stopTest();
    }
}

 
This was selected as the best answer
HTANIRSHTANIRS
Hi Raj,

This is working fine now. Code Coverage is 85% and deployed into production.
Thanks for your help.