0

I tried to populate PriceBookEntry for different currency in test class.It works for only one for one currency, when i try to add different currency it fail.

System.DmlException: Insert failed. First exception on row 1; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

Here is the code:

 //insert test product
   Product2 prod=new Product2(Name='SalesPrice',Family='SalesPrice');

   insert prod;


   // Get standard price book ID.
   // This is available irrespective of the state of SeeAllData.
   Id pricebookId = Test.getStandardPricebookId();

   system.debug('pricebookId = ' + pricebookId );

   //insert pricebookentry
   PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
    insert standardPrice;

   // Create a custom price book
    Pricebook2 customPB = new Pricebook2(Name='Standard Price Book', isActive=true);
    insert customPB;


    List<PricebookEntry> listPBE = new List<PricebookEntry>();
    PricebookEntry customPrice1 = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = prod.Id,CurrencyIsoCode ='GBP',
    UnitPrice = 0.0, IsActive = true);
    listPBE.add(customPrice1);

    PricebookEntry customPrice2 = new PricebookEntry(
    Pricebook2Id = customPB.Id, Product2Id = prod.Id,CurrencyIsoCode ='USD',
    UnitPrice = 0.0, IsActive = true);
    listPBE.add(customPrice2);

   insert listPBE;

CustomPrice1 is successfully inserted but customPrice2 is fail.

0

I found the solution whereby I need to define the currency one by one.

Example:

 List<Product2> listProd = new List<Product2>();
       Product2 prod=new Product2(Name='Radio',Family='Walkman',CurrencyIsoCode ='GBP');
       listProd.add(prod);

       Product2 prod1=new Product2(Name='Radio',Family='Walkman',CurrencyIsoCode ='USD');
       listProd.add(prod1);

       Product2 prod2=new Product2(Name='Radio',Family='Walkman',CurrencyIsoCode ='EUR');
       listProd.add(prod2);

       Product2 prod3=new Product2(Name='Radio',Family='Walkman',CurrencyIsoCode ='AUD');
       listProd.add(prod3);

       insert listProd;

       // Get standard price book ID.
       // This is available irrespective of the state of SeeAllData.
       Id pricebookId = Test.getStandardPricebookId();

        //insert pricebookentry for eacy currency
       List<PriceBookEntry> listStdPrice = new List<PriceBookEntry>();

        PricebookEntry standardPrice = new PricebookEntry(
                Pricebook2Id = pricebookId, Product2Id = prod.Id,
                UnitPrice = 0.0, IsActive = true);
        listStdPrice.add(standardPrice);

        PricebookEntry standardPrice1 = new PricebookEntry(
                Pricebook2Id = pricebookId, Product2Id = prod.Id,
                UnitPrice = 0.0, IsActive = true,CurrencyIsoCode ='USD');
        listStdPrice.add(standardPrice1);

        PricebookEntry standardPrice2 = new PricebookEntry(
                Pricebook2Id = pricebookId, Product2Id = prod.Id,
                UnitPrice = 0.0, IsActive = true,CurrencyIsoCode ='EUR');
         listStdPrice.add(standardPrice2);

           PricebookEntry standardPrice3 = new PricebookEntry(
                Pricebook2Id = pricebookId, Product2Id = prod.Id,
                UnitPrice = 0.0, IsActive = true,CurrencyIsoCode ='AUD');
        listStdPrice.add(standardPrice3);

         insert listStdPrice;

          // Create a custom price book
       Pricebook2 customPB = new Pricebook2(Name='Standard Price Book', isActive=true);
       insert customPB;

       //create custom Price book for each currency
       List<PriceBookEntry> listCustPrice = new List<PriceBookEntry>();
       PricebookEntry customPrice1 = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = prod.Id,CurrencyIsoCode ='GBP',
                                                UnitPrice = 0.0, IsActive = true,useStandardPrice=false);
       listCustPrice.add(customPrice1);

       PricebookEntry customPrice2 = new PricebookEntry(
       Pricebook2Id = customPB.Id, Product2Id = prod1.Id,
       UnitPrice = 0.0, IsActive = true,useStandardPrice=false,CurrencyIsoCode ='USD');
       listCustPrice.add(customPrice2); 

       PricebookEntry customPrice3 = new PricebookEntry(
       Pricebook2Id = customPB.Id, Product2Id = prod2.Id,
       UnitPrice = 0.0, IsActive = true,useStandardPrice=false,CurrencyIsoCode ='EUR');
       listCustPrice.add(customPrice3); 

        PricebookEntry customPrice4 = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = prod3.Id,CurrencyIsoCode ='AUD',
                                                UnitPrice = 0.0, IsActive = true,useStandardPrice=false);
       listCustPrice.add(customPrice4);

       insert listCustPrice;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.