You need to sign in to do that
Don't have an account?
Filipe Baradelli
Test.getStandardPricebookId() can only be called from test methods
How could I insert a product in Opportunity by the Apex (the problem is because the sentence is for tests, so how I do this?).
Please, help me.
public class Teste { public void teste1(){ // Insert Product Product2 pr = new Product2(); pr.Name='Moto - G1'; pr.isActive=true; insert pr; // Insert Pricebook PriceBook2 customPriceBook = new PriceBook2(); customPriceBook.Name='Custom Pricebook'; customPriceBook.IsActive=true; insert customPriceBook; // Query Standard and Custom Price Books Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id]; Id stdPriceBookRecId = Test.getStandardPricebookId(); // Create Standard PriceBookEntry PriceBookEntry stdPriceBookEntry = new PriceBookEntry(); stdPriceBookEntry.Product2Id=pr.Id; stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId; stdPriceBookEntry.UnitPrice=2000; stdPriceBookEntry.IsActive=true; insert stdPriceBookEntry; // Create Custom PriceBookEntry PriceBookEntry customPriceBookEntry = new PriceBookEntry(); customPriceBookEntry.Product2Id=pr.Id; customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id; customPriceBookEntry.UnitPrice=5000; customPriceBookEntry.IsActive=true; insert customPriceBookEntry; // Create Opportunity Opportunity opp = new Opportunity(); opp.Name = 'Test'; opp.CloseDate= System.Today(); opp.StageName='Prospecting'; insert opp; // Add product and Pricebook to the particular opportunity using OpportunityLineItem OpportunityLineItem oppLineItem = new OpportunityLineItem(); oppLineItem.OpportunityId = opp.Id; oppLineItem.PricebookEntryId = customPriceBookEntry.Id; oppLineItem.UnitPrice = 7000; oppLineItem.Quantity = 5; insert oppLineItem; } }
Please, help me.
It is simple clear: you are assigning the Pricebook2Id field (of yp ID) with a variable of type List<PriceBook2>, but you need instead to assign it to the [0] element and its Id field:
stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
All Answers
the Test.getStandardPricebookId() is available for test methods and not for normale execution.
To get the ID of the sandard pricebook you just need to make a single query:
Select Id,name,isstandard from Pricebook2 Where IsStandard=true
Hope this helps.
--
May the Force.com be with you!!
Illegal assignment from List<Pricebook2> to List<Integer> (at line 21 and column 9)
Here is the code now:
Now the problem is this :
Illegal assignment from List<Pricebook2> to Id (at line 27 and column 9)
Code:
It is simple clear: you are assigning the Pricebook2Id field (of yp ID) with a variable of type List<PriceBook2>, but you need instead to assign it to the [0] element and its Id field:
stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId[0].Id;
I had not enough attemtion for putting the [0] and the .Id separately.
I'm trying to do it for a long time and you helped me so much.
Tank you again and "May the Force.com be with you!".
Have a nice day!