+ Start a Discussion
Gage Staruch 9Gage Staruch 9 

Can someone help me write a test class?

I need some help writing a test class. I keep encountering errors with my existing class and would appreciate a fresh view. 

I basically need this re-written:

@isTest
private class ActiveAccountTest{
    static testmethod void validateActiveAccount()
    {
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;     
        
        Opportunity oppObj = new Opportunity(Name = 'TestOpp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='A - Purchase / Close Won', MarketingGeneratedType__c = 'Sales');
		insert oppObj;

		Product2 newProd = new Product2 (Name = 'Product 1', ProductLine__c ='Custom Products');
		insert newProd;

		PriceBookEntry pbEntry = new PriceBookEntry(
    	UnitPrice = 1,
    	PriceBook2Id = [SELECT Product2.Id, Product2.Name FROM PriceBookEntry WHERE Pricebook2Id IN (SELECT Id FROM PriceBook2 WHERE Name = 'Custom Products') LIMIT 1].Id,
    	Product2Id = newProd.Id,
   		IsActive = true);

		insert pbEntry ;

		OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000,Quantity = 1,OpportunityID = oppObj.Id);
		insert oppLine;
        
        Test.startTest();
            Account acct4 = [Select Custom_Checkbox_1 from Account limit 1];
        Test.stopTest();
         }
}

The issue here is that I need to reference a custom Pricebook ID. This code fails my code coverage tests and has this error:

System.QueryException: List has no rows for assignment to SObject

Marzorati ClaudioMarzorati Claudio
Hi Gage,

in test class only Standard Pricebook exists.
That means that you must re-create custom pricebooks in test class in order to use them.

Claudio
Gage Staruch 9Gage Staruch 9

Thank you @Marzorati!

 

Could you show me what that would look like?

Marzorati ClaudioMarzorati Claudio
For sure.

here code
Pricebook2 pb = new Pricebook2(
        Name = 'Custom PB',
        IsActive = TRUE
);
insert pb;
If you need some custom fields fill the code for your goal

Please mark as best response if you solve the problem
Claudio
Gage Staruch 9Gage Staruch 9

Thanks @Marzorati!

 

I am now seeing the below error though

 

System.DmlException: Insert failed. First exception on row 0 with id 01s1g000001VnNRAA0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Marzorati ClaudioMarzorati Claudio
Hi Gage,

it seems that you specify an Id on the attempt to create a record.
That is not allowed for INSERT call, but only on UPDATE or UPSERT calls.

Please post your code in order to help you
Claudio
Gage Staruch 9Gage Staruch 9

Hi @Marzorarti, 

 

Sorry for the late response. Here is the updated code:

 

@isTest
private class ActiveAccountTest{
    static testmethod void validateActiveAccount()
    {
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;     
        
        Opportunity oppObj = new Opportunity(Name = 'TestOpp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='A - Purchase / Close Won', MarketingGeneratedType__c = 'Sales');
		insert oppObj;

		Product2 newProd = new Product2 (Name = 'Custom Product', ProductLine__c ='Custom Product Line');
		insert newProd;
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
            insert customPB;

		PriceBookEntry pbEntry = new PriceBookEntry(
    	UnitPrice = 1,
        PriceBook2Id = [SELECT Id FROM PriceBook2 WHERE Name = 'Custom Pricebook'].id,

    	Product2Id = newProd.Id,
   		IsActive = true);

		insert pbEntry ;

		OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000,Quantity = 1,OpportunityID = oppObj.Id);
		insert oppLine;
        
        Test.startTest();
            Account acct4 = [Select Custom_Checkbox__c from Account limit 1];
        Test.stopTest();
         }
}
Marzorati ClaudioMarzorati Claudio
Hi Gage,

first of all you have two times the line insert customPB; (line 18)

Then you have to insert a standard price in order to use a custom pricebook in your scenario.
 
To be used in custom price books, all standard prices must be added as price book entries to the standard price book.

Follow this code to your test
 
// Create your test data
Account acc = new Account();
acc.name= 'test';
insert acc;

Opportunity oppObj = new Opportunity(Name = 'TestOpp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='A - Purchase / Close Won');
insert oppObj;
Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
insert pb;
Product2 prod = new Product2(Name = 'SLA: Bronze', IsActive = true);
insert prod;
// First insert a price for the standard price book
Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
PricebookEntry standardPBE = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
insert standardPBE;
PricebookEntry pbEntry = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
insert pbEntry;

OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000,Quantity = 1,OpportunityID = oppObj.Id);
insert oppLine;

Please mark as best answer if you solve the problem
Claudio