You need to sign in to do that
Don't have an account?
SQOL Query get PriceBookEntryId using opportunity info
Hello,
I'm stuck on a SOQL query, I want to get the pricebookentryid in a single query to square some selects.
I have this :
Id idProduit = [SELECT Id, Produit__c
FROM Opportunity
WHERE Id =: opp.id].Produit__c;
String codeProduit = [SELECT Id, ProductCode
FROM Product2
WHERE Id =: idProduit].ProductCode;
Id idPbe = [SELECT Id
FROM PriceBookEntry
WHERE ProductCode=: codeProduit
AND CurrencyIsoCode =: opp.CurrencyIsoCode].Id;
Is it possible to resume it in a single query ?
Thanks for your help.
This still uses 2 queries, but is concise:
* pbe.Product2Id is the same as Opp.Produit__c
* pbe.ProductCode is the same as Opp.Produit__r.ProductCode
* pbe.Id is the final result you are looking for.
All Answers
You can use Parent and Child Relationship Queries to minimize the number of SOQL queries.
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
In your example, assuming you have a lookup to Product from Opportunity, you could do this in 2 queries instead of 3.
Opportunity oppty = [Select Id,Name, Product_r.ProductCode, Product__c from Opporunity where Id =: opp.Id limit 1];
PriceBookEntry pbe = [Select Id from PriceBookEntry where ProductCode =: oppty.Product__r.ProductCode and CurrencyIsoCode =: opp.CurrencyIsoCode limit 1];
This still uses 2 queries, but is concise:
* pbe.Product2Id is the same as Opp.Produit__c
* pbe.ProductCode is the same as Opp.Produit__r.ProductCode
* pbe.Id is the final result you are looking for.