1

My test class:

@isTest
private class opplineitemHandlerTest {
    
    @isTest
    static void testCalculateTotal() {
        // Create test data
        Pricebook2 standardPricebook = [SELECT Id,Name FROM Pricebook2 WHERE IsStandard = true];
        //Pricebook2 standardPricebook = new Pricebook2();
        system.debug(standardPricebook);
        Product2 testProduct = new Product2(Name = 'Test Product', IsActive = true);
        insert testProduct;
        
        PricebookEntry standardPricebookEntry = new PricebookEntry(Pricebook2Id = standardPricebook.Id, Product2Id = testProduct.Id, UnitPrice = 100, IsActive = true);
        insert standardPricebookEntry;
        
        Opportunity testOpp = new Opportunity(Name = 'Test Opportunity', StageName = 'Prospecting', CloseDate = Date.today());
        insert testOpp;
        
        OpportunityLineItem testLineItem1 = new OpportunityLineItem(OpportunityId = testOpp.Id, Quantity = 1, TotalPrice = 100, PricebookEntryId = standardPricebookEntry.Id);
        OpportunityLineItem testLineItem2 = new OpportunityLineItem(OpportunityId = testOpp.Id, Quantity = 2, TotalPrice = 100, PricebookEntryId = standardPricebookEntry.Id);
        List<OpportunityLineItem> testLineItems = new List<OpportunityLineItem>{testLineItem1, testLineItem2};
        insert testLineItems;
        
        // Call the method to be tested
        Test.startTest();
        opplineitemHandler.calculateTotal(testLineItems);
        Test.stopTest();
        
        // Verify the results
        Opportunity updatedOpp = [SELECT Id, Total_Line_Item_Value__c FROM Opportunity WHERE Id = :testOpp.Id];
        System.assertEquals(400, updatedOpp.Total_Line_Item_Value__c);
    }
}

I'm getting an error like 'List has no rows for assignment to SObject' on the Pricebook2 SOQL query. Could you please help me resolve this?

1
  • You need to make sure that you create the data being used in the test at the start of the test or in a @TestSetup method. (Or you use @SeeAllData on the test class, but that is an anti-pattern.)
    – Phil W
    Commented Jun 6 at 11:03

1 Answer 1

1

You can use the Test.getStandardPricebookId(); method to get the ID of the standard price book in your organization.

@isTest
private class opplineitemHandlerTest {
    
    @isTest
    static void testCalculateTotal() {
        // Create test data
        Id pricebookId = Test.getStandardPricebookId();
        system.debug(pricebookId);
        Product2 testProduct = new Product2(Name = 'Test Product', IsActive = true);
        insert testProduct;
        
        PricebookEntry standardPricebookEntry = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = testProduct.Id, UnitPrice = 100, IsActive = true);
        insert standardPricebookEntry;
        
        Opportunity testOpp = new Opportunity(Name = 'Test Opportunity', StageName = 'Prospecting', CloseDate = Date.today());
        insert testOpp;
        
        OpportunityLineItem testLineItem1 = new OpportunityLineItem(OpportunityId = testOpp.Id, Quantity = 1, TotalPrice = 100, PricebookEntryId = standardPricebookEntry.Id);
        OpportunityLineItem testLineItem2 = new OpportunityLineItem(OpportunityId = testOpp.Id, Quantity = 2, TotalPrice = 100, PricebookEntryId = standardPricebookEntry.Id);
        List<OpportunityLineItem> testLineItems = new List<OpportunityLineItem>{testLineItem1, testLineItem2};
        insert testLineItems;
        
        // Call the method to be tested
        Test.startTest();
        opplineitemHandler.calculateTotal(testLineItems);
        Test.stopTest();
        
        // Verify the results
        Opportunity updatedOpp = [SELECT Id, Total_Line_Item_Value__c FROM Opportunity WHERE Id = :testOpp.Id];
        System.assertEquals(400, updatedOpp.Total_Line_Item_Value__c);
    }
}

For the more information refer to this documentation.

2
  • 1
    Another option (for those who have/use more than just the standard pricebook) is to upsert new Pricebook2(Name = "My Pricebook") Name; The Pricebook name field has the idLookup property, so we can specify that in an upsert to have Salesforce find and update the existing pricebook. Through some magic, that makes the pricebook queryable (after doing the upsert) in test contexts.
    – Derek F
    Commented Jun 6 at 11:39
  • 1
    Thank you Tushar Jadav. It's working fine.
    – Sridhar
    Commented Jun 6 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .