You need to sign in to do that
Don't have an account?

Pricebook2Id
I am looking for help.
I have written a test program for an Apex trigger. The program creates a test account and an opportunity
that is associated with it. The code is shown below:
// Create a test account for 'Corp Admin' Account testAccount = new Account ( Name = 'Test Account', Owner = [select username from user where username = 'Corp Admin'] ); insert testAccount; // Create a forecast opportunity Opportunity testOpp = new Opportunity ( AccountId = testAccount.ID, Name = 'testOpportunity' + date.today(), CloseDate = date.today() + 1, StageName = 'Contract signed; waiting for P.O', Type = 'Existing Business', Pricebook2Id = [select Id from Pricebook2 where name = 'Standard Price Book'], OwnerId = testAccount.OwnerId ); insert testOpp;
In the program, I want to set the appropriate Price Book, so that I can later enter line items from a specific price book.
The line:
Pricebook2Id = [select Id from Pricebook2 where name = 'Standard Price Book']
is used for this purpose.
When compiling, I get the error:
Invalid initial expression type for field Opportunity.Pricebook2Id, expecting: Id.
How would I get the Id for the price book?
In the same way, I am using the following, to enter a line item (OpportunityLineItem) for the opportunity:
PricebookEntryId = [select Id from Product2 where name = 'AC02'],
I get the same kind of error.
Thanks,
aqueller
You need to specify that you want to pass in the id field of the record returned by the query.
ie:
Pricebook2Id = [select Id from Pricebook2 where name = 'Standard Price Book'].id
All Answers
You need to specify that you want to pass in the id field of the record returned by the query.
ie:
Pricebook2Id = [select Id from Pricebook2 where name = 'Standard Price Book'].id
Thank you, it worked.
Turns out that I had to do the same for the creation of the account as well:
// Create a test account for 'Corp Admin'
Account testAccount = new Account
(
Name = 'Test Account',
OwnerId = [SELECT Id FROM user WHERE name = 'Corp Admin' LIMIT 1].Id
);
insert testAccount;