function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Filipe BaradelliFilipe 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?).
 
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.
Best Answer chosen by Filipe Baradelli
EnreecoEnreeco
You should watch line 27 and see the errore by yourself :)
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

EnreecoEnreeco
Hola Felipe,
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!!
Filipe BaradelliFilipe Baradelli
Thank you by the help. Now my problem is the error : 
Illegal assignment from List<Pricebook2> to List<Integer> (at line 21 and column 9)

Here is the code now:
public with sharing class Teste {

    public void teste1(){
        integer stdPriceBookRecId2; 
        List<Integer> stdPriceBookRecId = new List<Integer>();

        // 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];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
        stdPriceBookRecId2 = stdPriceBookRecId[0]; 
        
        // Create Standard PriceBookEntry
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=pr.Id;
        stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId2;
        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;
    }
}

 
EnreecoEnreeco
Replace line 05  with:
List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();

 
Filipe BaradelliFilipe Baradelli
I did not realize the Pricebook2 (rsrs)
Now the problem is this : 
Illegal assignment from List<Pricebook2> to Id (at line 27 and column 9)

Code:
public with sharing class Teste {

    public void teste1(){
        integer stdPriceBookRecId2; 
        List<Pricebook2> stdPriceBookRecId = new List<Pricebook2>();

        // 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];
        stdPriceBookRecId = [SELECT Id, isstandard FROM Pricebook2 WHERE IsStandard=true];
    //    stdPriceBookRecId2 = stdPriceBookRecId[0]; 
        
        // 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;
    }
}

 
EnreecoEnreeco
You should watch line 27 and see the errore by yourself :)
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;
This was selected as the best answer
Filipe BaradelliFilipe Baradelli
THANK YOU !!!
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!".
EnreecoEnreeco
I'm happy I've helped you out!
Have a nice day!
Filipe BaradelliFilipe Baradelli
Now I will. And have a nice day to you too.