You need to sign in to do that
Don't have an account?
Gage 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
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
Thank you @Marzorati!
Could you show me what that would look like?
here code If you need some custom fields fill the code for your goal
Please mark as best response if you solve the problem
Claudio
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]
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
Hi @Marzorarti,
Sorry for the late response. Here is the updated code:
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.
Follow this code to your test
Please mark as best answer if you solve the problem
Claudio