+ Start a Discussion
Yashasvi DYashasvi D 

PricebookEntry test class CodeCoverage failing

Hi ,

I am unable to complete testclass for PricebookEntry Object, which is failing because i need to populate PricebookEntry's ProductCode field(not writable field) in my test class. 
Condition in main class is :- "select id from PricebookEntry where productcode =:Productname"

I also checked with @isTest(seeAllData=True) in my test class. but no improvement.

Can anyone please help me on the above issue.

Thanks!!

KR,
Yash's.
 
chanchal_:)chanchal_:)
https://developer.salesforce.com/forums/?id=906F0000000BIGWIA4
Ajay K DubediAjay K Dubedi
Hi Yashasvi,

Use this test class for PricebookEntry helpful for you.
 
@isTest
private class AllAccountHave100QuantityTest {
    @isTest static void AccountShow()
    {
        Test.startTest();
        Account accObj = New Account();
        accObj.Name='Acc1';
        accObj.Ammount__c =0;
        accObj.NumberOfEmployees=1;
        Insert accObj;
        
        Opportunity opp = New Opportunity();
        opp.Name='opp1';
        opp.StageName='Closed Won';
        opp.AccountId=accObj.Id;
        opp.CloseDate=Date.today()+2;
        Insert opp;
        
        Product2 pro= New Product2();
        pro.Name='mango';
        Insert pro;
        
        Pricebook2 pr = new Pricebook2();
        pr.Name='Jhon';
        pr.IsActive=True;
        Insert pr;
        String prID2 = Test.getStandardPricebookId();
        
        PricebookEntry pre=New PricebookEntry();
        pre.Pricebook2Id=prID2;
        pre.Product2Id=pro.Id;
        pre.UnitPrice=2;
        pre.isActive=true;
        Insert pre;
        
        
        OpportunityLineItem olii = new OpportunityLineItem (); 
        olii.ServiceDate=date.today(); //system.today()+5;
        olii.Quantity=101;
        olii.OpportunityId=opp.Id;
        olii.TotalPrice=10;
        olii.priceBookEntryId=pre.Id;
        try {
            insert olii;
        } catch(Exception Ex) {
            system.debug('Error is ::: ' + Ex.getLineNumber() + ' Due to :: ' + Ex.getMessage());
        }
        
        AllAccountHave100Quantity.ShowAccount();
        Test.stopTest();
    }
    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com