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
Swaroopa Akula 2Swaroopa Akula 2 

Code coverage is not enough for Test class- opportunty line item insertion

Here is my trigger to add a opportunity line item in the oppty.

Set<id> oId = new set<id>();
    for(Opportunity opp : trigger.new){
        oId.add(opp.Id);
    }
    
    Opportunity opp = [select Id ,NRR_value__c ,CurrencyIsoCode,RecordTypeId,recordtype.name from Opportunity where ID =: oId];
    system.debug('opp************'+opp);
     
    PriceBookEntry p = [SELECT Id, Product2Id, Product2.Id, Product2.Name, CurrencyIsoCode FROM PriceBookEntry WHERE 
                        Product2Id='01t3O000003UXKsQAO' and CurrencyIsoCode =: opp.CurrencyIsoCode and Pricebook2Id = '01s0J000002SCKDQA4'];
    system.debug('p***************'+p);
    
    string recordtypename = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(opp.recordtypeid).getname();
    system.debug(' recordtypename*************'+recordtypename);
    
    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
    for(Opportunity op : trigger.new){
        if(opp.RecordType.Name == 'Services Record Type'){
             OpportunityLineItem oli = new OpportunityLineItem();
             oli.OpportunityId = opp.Id;
             oli.Quantity = 1;
             oli.PricebookEntryId = p.Id;
             //oli.name = opp.name;
             
             oli.UnitPrice = opp.NRR_value__c;
             oliList.add(oli);
            system.debug('oliList------------'+oliList);
        }   
     }
     insert oliList;



Test class for this trigger:



@isTest(SeeAllData=true)
public class insertopplineitemTest{
    static testMethod void insertopptylineitemTest() {
        Account acct = new Account(Name = 'Celigo testing account',Billingcountry = 'United States');
        insert acct;
       
        //PriceBook Standard
        PriceBook2 stdPBook = [ SELECT Id, IsActive FROM PriceBook2 WHERE IsStandard = True AND IsActive = TRUE LIMIT 1 ];
       
        //Pricebook Custom
        PriceBook2 custPBook = new PriceBook2( Name = 'Test Pricebook',Description = 'Custom Pricebook',CurrencyIsoCode = 'USD',IsActive = TRUE );
        insert custPBook;
       
        //Product
        Product2 prod = new Product2( Name = 'Celigo Test Product',CurrencyIsoCode = 'USD',isActive = TRUE );
        insert prod;
       
        //PriceBookEntry Standard
        PricebookEntry pbEntry1 = new PricebookEntry( Pricebook2Id = stdPBook.Id,Product2Id = prod.id,CurrencyIsoCode = 'USD',IsActive = TRUE );
        pbEntry1.UnitPrice = 100.0;
        pbEntry1.UseStandardPrice = FALSE;
        insert pbEntry1;
           
        PricebookEntry pbEntry2 = new PricebookEntry( Pricebook2Id = custPBook.Id,Product2Id = prod.id,CurrencyIsoCode = 'USD',IsActive = TRUE );
        pbEntry2.UnitPrice = 200.0;
        pbEntry2.UseStandardPrice = FALSE;
        insert pbEntry2;
       
        
        // Opportunity
        Opportunity oppy = new Opportunity( Name = 'Celigo Test Opportunity',AccountId = acct.Id,CurrencyIsoCode = 'USD',NRR_value__c=100);
        
        //oppy.RecordTypeId=Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Services_Record_Type').getRecordTypeId();
        oppy.StageName = '0 - Meeting';
        oppy.CloseDate = System.Today()+10;
     
        insert oppy;
           
        // OpportunityLineItem
        OpportunityLineItem opItem1 = new OpportunityLineItem(OpportunityId = oppy.Id, PricebookEntryId = pbEntry1.id);
        opItem1.UnitPrice = 100;
        opItem1.Quantity = 10;
        
        
        insert opItem1;
      
      Test class is passing but I am not able to get code coverage more than 75%. Whatelse we can add in the test class to get more code coverage? 
AbhishekAbhishek (Salesforce Developers) 
Hi,

Increase code coverage

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines


https://salesforce.stackexchange.com/questions/24165/why-is-75-code-coverage-required-in-salesforce/24167#24167


Try the suggestions as mentioned in the above discussions.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Swaroopa Akula 2Swaroopa Akula 2
Test case is covering code 100% in developer console but failing in text execution. I did tried clear histroy and run it but still same error.
Getting below error:
caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]

Below is the test class:
        
@isTest(SeeAllData=true)
public class insertopplineitemTest{
    static testMethod void insertopptylineitemTest() {
     
        Account acct = new Account(Name = 'Celigo testing account',Billingcountry = 'United States');
        insert acct;
       
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

        Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk1;

        Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
        insert prd1;

        PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,CurrencyIsoCode ='USD');
        insert pbe1;

        Opportunity opp1 = new Opportunity (Name='Opp1',CloseDate=Date.today(), AccountId = acct.id,Pricebook2Id = pbe1.Pricebook2Id);
       Id recordTypeId =Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('Services_Record_Type').getRecordTypeId();
        
        opp1.RecordTypeId=recordTypeId;
        opp1.StageName = '0 - Meeting';
        insert opp1;
        

        OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4, totalprice=200);
        insert lineItem1;
        
        
        
    }
}