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
Deepak PansariDeepak Pansari 

How do i get Product Name for an Opportunity?

Hi,

 

i want to get all the product name related with an Opportuntiy.

 

i want to write one trigger for that.

 

Does any body know the SQL (how do i query to salesforce) or any Idea.

 

 

Regards,

Deepak 

Best Answer chosen by Admin (Salesforce Developers) 
Guyver118Guyver118

public static void GetLineItems(List<Opporunity> opps) { Set<Id> oppIds = new Set<Id>(); for(Opporunity a : opps) { if(a.HasOpportunityLineItem) oppIds.add(a.Id); } Map<Id, List<OpportunityLineItem>> lineItems = new Map<Id, List<OpportunityLineItem> ([Select o.PricebookEntry.Name, o.PricebookEntryId, o.OpportunityId From OpportunityLineItem o Where o.OpportunityId In :oppIds]); for(Opporunity a : opps) { for(OpportunityLineItem b : Map.get(a.Id) { System.debug(b.PricebookEntry.Name); } } }

 Try this might work?

All Answers

Guyver118Guyver118

public static void GetLineItems(List<Opporunity> opps) { Set<Id> oppIds = new Set<Id>(); for(Opporunity a : opps) { if(a.HasOpportunityLineItem) oppIds.add(a.Id); } Map<Id, List<OpportunityLineItem>> lineItems = new Map<Id, List<OpportunityLineItem> ([Select o.PricebookEntry.Name, o.PricebookEntryId, o.OpportunityId From OpportunityLineItem o Where o.OpportunityId In :oppIds]); for(Opporunity a : opps) { for(OpportunityLineItem b : Map.get(a.Id) { System.debug(b.PricebookEntry.Name); } } }

 Try this might work?

This was selected as the best answer
ron_reedron_reed
I'm trying to do something similar but I get an Invalid nested collection type error on the Select line.  Any ideas?
Deepak PansariDeepak Pansari

Can you post code or actualerror message which you are getting?

 

 

ron_reedron_reed

Map<Id, List<OpportunityLineItem>> lineItems = new Map<Id, List<OpportunityLineItem> ([Select o.PricebookEntry.Name, o.PricebookEntryId, o.OpportunityId From OpportunityLineItem o Where o.OpportunityId In :oppIds]);

 

This is the error I'm getting (In Eclipse) -

Save error: Invalid nested collection type

 

There is no error code associated with it.


Thanks! 

Message Edited by ron_reed on 02-02-2009 08:04 AM
Guyver118Guyver118

i think the error is in the map syntax try this

 

 

Map<Id, List<OpportunityLineItem>> lineItems = new Map<Id, List<OpportunityLineItem>>([Select o.PricebookEntry.Name, o.PricebookEntryId, o.OpportunityId From OpportunityLineItem o Where o.OpportunityId In :oppIds]);

 

You will get another error about the invalid initial type. The problem is the mapping of the list in this sql statement though