+ Start a Discussion
Roger WickiRoger Wicki 

Efficient query for PricebookEntry

Dear community

We have a scheduled daily job, that clones certain Opportunities. But of course, we also need the OpportunityLineItems (OLI) cloned. In addition, we need no simple clone of the OLI, but we need them to use the same price, same quantity, same currency, same product, but of the "next pricebook". now we have a custom field on Pricebook, that holds a reference to the "next pricebook". So getting that Id in my apex class is fine. I also know how to query the new PricebookEntry records for the cloned OLIs, but I don't know if there is a more efficient way.

User-added image

In the end all I need to query is the PricebookEntryId of the greyed out one. Currently, I use a query that just returns every PricebookEntry of every Currency of all used Products and Pricebooks within the Trigger Context. For large execution context, this query can easily bea really slow and heavy. I wanted to know if there is a more optimal approach without needing to use more loops over collections or more individual queries.

Maybe someone can help me (or say it's not possible).