+ Start a Discussion
Varun99Varun99 

SOQL

Hi,

 

 How to get all products using opportunity id through soql?

 

any one help me?

 

Thanks in advance

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC
Hi,

You need to do a query on OpportunityLineItem and then retrieve all the products.

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_opportunitylineitem.htm

sET<Id> productIds = new Set<Id>();

for(OpportunityLineItem objOLI : [Select Id, ProductId, OpportunityId from Opportunity where OpportunityId = : 'Id Of Opportinity Goes Here']){
if(objOLI.ProductId != null){
productIds.add(objOLI.Id);
}

}

List<Product2> productList = [Select Id from Product2 where Id In: productIds];

System.debug('Product List --->'+productList);

Hope this helps :)

Thanks,
Devendra

All Answers

Devendra@SFDCDevendra@SFDC
Hi,

You need to do a query on OpportunityLineItem and then retrieve all the products.

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_opportunitylineitem.htm

sET<Id> productIds = new Set<Id>();

for(OpportunityLineItem objOLI : [Select Id, ProductId, OpportunityId from Opportunity where OpportunityId = : 'Id Of Opportinity Goes Here']){
if(objOLI.ProductId != null){
productIds.add(objOLI.Id);
}

}

List<Product2> productList = [Select Id from Product2 where Id In: productIds];

System.debug('Product List --->'+productList);

Hope this helps :)

Thanks,
Devendra

This was selected as the best answer
Varun99Varun99

Hi Devendra,

 

   Thanks for your reply. It helped for my requirement..