+ Start a Discussion
anirbasdevanirbasdev 

Opportunities related to OpportunityLineItem

I have a request to create a visualforce page on the OpportunityLineItem Object that display all the opportunities associated to the same product as this OpportunityLineItem
 
bhanu_prakashbhanu_prakash
Hi AnirBasdev,

Mark as best answer, If it resloves !!​​​​​​​​
<apex:page controller="myOpptyController" tabStyle="Opportunity">
    
    <apex:pageBlock title="opportunity product related list">

    <apex:pageBlockTable value="{!opptyList}" var="div">
          
          <apex:column >
                    <apex:pageBlockTable value="{!div.OpportunityLineItems}"  var="custom">
                    <apex:column value="{!custom.Quantity}"/>
                    <apex:column value="{!custom.UnitPrice}"/>
                    <apex:column value="{!custom.TotalPrice}"/>
                    <apex:column value="{!custom.PricebookEntry.Name}"/>
                    <apex:column value="{!custom.PricebookEntry.Product2.Family}"/>
                </apex:pageBlockTable>
        </apex:column>
          
          
 </apex:pageBlockTable>
</apex:pageBlock>
    
</apex:page>
Controller :
 
public class myOpptyController {

    public List<Opportunity> opptyList;
    
    public myOpptyController() {

    opptyList = [SELECT Id,Name,Account.Name, 
                                  (SELECT Quantity, UnitPrice, TotalPrice,PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems) 
                        FROM Opportunity WHERE Id =: ApexPages.currentPage().getParameters().get('opptyID')];
    
        System.debug('opptyList ='+opptyList);

    }
    
    public List<Opportunity> getopptyList() {
        return opptyList;
    }
    
}

Mark as resloved if it helps :) :)​
Thanks, 
Bhanu Prakash
visit ForceLearn.com

 
anirbasdevanirbasdev
hi Bhanu Prakash thank you for your answer,
i need to display all related opportunity record  to a specific product2 and integrate the visualforce page on the page layout of the Product2 object by using a standard controller "Product2" and controller extension 
Ajay K DubediAjay K Dubedi
Hi Anirbasdev,
Try this code, In this code, you can see the ProductId and it's related Opportunities Id.
If you want to get their name also then change the code according to your requirement.
VF Page:
<apex:page controller = "GetRelatedOpportunity">
    <apex:pageBlock title="opportunity product related list">
        <apex:pageBlockTable value="{!wrapList}" var="div">
            <apex:column value="{!div.ProductId}"/>
            <apex:column value="{!div.OppLIstId}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Controller :
public class GetRelatedOpportunity {
    public List<Wrapper> wrapList {get;set;}
    public GetRelatedOpportunity() {
        try {
            wrapList = new  List<Wrapper>();
            Map<Id, List<Id>> productIdVsOppMap = new Map<Id, List<Id>>();
            List<OpportunityLineItem> oliLIst = new List<OpportunityLineItem>();
            oliLIst = [SELECT Id, Product2Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId != null LIMIT 10000];
            if(oliLIst.size() > 0) {
                for(OpportunityLineItem oli : oliLIst) {
                    if(!productIdVsOppMap.containsKey(oli.Product2Id)) {
                        productIdVsOppMap.put(oli.Product2Id, new List<Id>());
                        productIdVsOppMap.get(oli.Product2Id).add(oli.OpportunityId);
                    }
                    else
                    {
                        productIdVsOppMap.get(oli.Product2Id).add(oli.OpportunityId);
                    }
                }
            }
            if(!productIdVsOppMap.isEmpty()) {
                for(Id i : productIdVsOppMap.keySet()) {
                    Wrapper w = new Wrapper();
                    w.ProductId = i;
                    w.OppLIstId = productIdVsOppMap.get(i);
                    wrapList.add(w);
                }
            }
        }  catch(Exception e){
            system.debug('LineNumber------->'+e.getLineNumber()+' Message------->'+e.getMessage());
        }
    }
    
    public class Wrapper {
        
        public String ProductId {
            get;set;
        }
        
        public List<Id> OppLIstId {
            get;set;
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi