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
Mark.MulhollandMark.Mulholland 

SOQL Query on PriceBookEntry not working

Hello,

 

I am trying to create a trigger on a custom object called Price Plan. The Trigger should query the PriceBookEntry object and, using the associated Product Id, The Account's Currency ISO Code and a hard coded Price Book Id. retrieve the UnitPrice and enter in into the List Price field on the Price Plan.

 

I could get the trigger to save but I am getting an Index Out Of Bounds error when I save a Price Plan which presumably means the SOQL Query is not getting any results to populate the UnitPrices List. However I tested this SOQL Query in the Dataloader and it worked. Can someone let me know where I am going wrong please

 

I know the code is not pretty, I haven't been coding long :/

 

 

 

 

trigger getPriceBookEntry on Price_Plan__c (before insert, before update) {


    Id[] productIds = new List<Id>();
    string[] currencyISOs = new List<string>();
    double[] unitPrices = new List<double>();
    Integer i = 0;
    //declare variables
    
    
    
    for (Price_Plan__c p : trigger.new) {
               productIds.set(i,p.product__r.id);
               currencyISOs.set(i,p.account__r.CurrencyIsoCode);
               i++;
               //iterate through all new Price Plans and each time insert the
               //product Id's and Currency ISO's for each Price Plan into the lists
               }
               
    
    
          i = 0;
          for (PriceBookEntry pbu : [Select UnitPrice from pricebookEntry where PriceBook2Id = '01sL0000000CoyR'
                                      AND currencyISOCode = :currencyISOs.get(i)
                                      AND product2id = :productIds.get(i)]) {
               unitPrices.set(i,pbu.UnitPrice);
               i++;
               //perform a SOQL query to pull the corresponding UnitPrice from the PriceBookEntry object where the
               //ProductId and CurrencyISO's match up. Then insert the Unit Price into the UnitPrices list
       }
         
         
         
    
    i = 0;
    for (price_plan__c p : trigger.new) {
        p.List_Price__c = unitPrices.get(i);
        i++;
        //Loop through all Price Plans again, this time populating the List_Price__c field with the
        //associated UnitPrice
        }
         
         
         
               
}

craigmhcraigmh

1) you shouldn't use hard-coded IDs in test methods

2) you shouldn't perform SOQL queries inside of loops

3) http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit

Mark.MulhollandMark.Mulholland

Thanks very much for the reply.

 

Normally I wouldn't use a hard coded Id, however in this case I wanted to see if the rest of the code would work. I was not sure if it was possible to query the PriceBookEntry object like that, and I didn't want to worry that a different SOQL query (to get the PriceBook Id) was the problem.

 

As for the SOQL Query inside the for loop, I used that method because I couldn't think of another way to store the ProductId and Currency ISO Code variables except for in their own Lists. In order to reference the variables incrementally for each new Price Plan I needed to use the i variable which increments in each loop. Otherwise I would have populated the list with a SOQL query when I declared it.

 

Thanks

Mark