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
Glen.ax1034Glen.ax1034 

SOQL Compilation Query Assistance

 

I am trying to write a packageextensions class that will return a list of Product2 so that I can allow the user to choose which products go into a package. I finally figured out how they are all linked together, but my SOQL skills and LIST skills are not up to the level required to write this. Here is how it all links together:

 

Part 2 is a list, I recognize that. I need to convert these SOQL statements so that they all go together and result in a list of products. Any ideas? Are these nested SOQL statements? Do I need mapping?

 

First:
	Select Opportunity__c from Packages__c where id='a3FT00000009YHV'
		returns:
		'006T0000008rbgtIAA' which is the opportunity associated with the package. every package only has one opportunity.

Second:
	Select Id from OpportunityLineItem where OpportunityId = '006T0000008rbgtIAA'
		returns:
		'00kT0000006XzBMIA0'
		'00kT0000006Z6csIAC'
		'00kT0000006YgMHIA0'
		'00kT0000006YWuqIAG'

Third:
	Select PricebookEntryId from OpportunityLineItem where id='00kT0000006XzBMIA0'
		returns:
		'01u70000003P4sNAAS' (which is the productcode id)

Lastly:
	Select Product2Id from PricebookEntry where id='01u70000003P4sNAAS'
		returns:
		'01t700000017VVBAA2' (which is the product2 id, hurray!)

Confirm:
	Select Name from Product2 where id='01t700000017VVBAA2'
		--- this returns the product name. score!

 

 

 

 

public class packageextensions {
    private final Packages__c PackageObj;
    public packageextensions (ApexPages.StandardController controller) {
        this.PackageObj= (Packages__c)controller.getSubject();
    }
    
    public List<Product2> getProducts() {
        
        //Opportunity op = [Select PackageObj.Opportunity from Package__c];
        
        //List<Product2> products = [select id from OpportunityLineItem where Opportunity__c =:op.id];
        
        return products;
        
        }
        

}

 

 

Abhinav GuptaAbhinav Gupta

Not sure if understood the question correctly. Are you trying to develop a managed package extension, where extension needs to pass some detail to parent package class ?

Glen.ax1034Glen.ax1034

Great question. I am trying to plug it all into a visualforce page:

 

The idea is that when I pick a specific package, it pulls from the opportunity that it is linked to all of the products presently listed in that opportunity so that I can add them to a package.

 

Thoughts?

 

<apex:page standardController="Packages__c" extensions="packageextensions">

<apex:listViews type="Product2" />


</apex:page>

 

Glen.ax1034Glen.ax1034

Getting closer...

 

SELECT Amount, Id, Name, (SELECT Quantity, ListPrice,
  PriceBookEntry.UnitPrice, PricebookEntry.Name,
  PricebookEntry.product2.Family FROM OpportunityLineItems)
  FROM Opportunity

Glen.ax1034Glen.ax1034
public class packageextensions {
    private final Packages__c PackageObj;
    public packageextensions (ApexPages.StandardController controller) {
        this.PackageObj= (Packages__c)controller.getSubject();
    }
    
    public List<Product2> getProducts() {
    
        List<Product2> returnProducts = new List<Product2>();
        
        List<Packages__c> origPackage = new List<Packages__c>();
        origPackage = [select Opportunity__c from Packages__c where id=:PackageObj.Id Limit 1];
        
        List<OpportunityLineItem> origOpportunityLineItem = new List<OpportunityLineItem >();
        origOpportunityLineItem = [Select PricebookEntryId from OpportunityLineItem where OpportunityId =:origPackage[0].Opportunity__c];
        
        
        for(OpportunityLineItem presentopp : origOpportunityLineItem) {
            List<PricebookEntry> pbe = new List<PricebookEntry>();
            pbe = [Select Product2Id from PricebookEntry where id=:presentopp.PricebookEntryId];
            
            List<Product2> productitem = new List<Product2>();
            productitem = [Select Name, id from Product2 where id=:pbe[0].Product2Id];
              
            returnProducts.add(productitem[0]);
        
        }
        
        return returnproducts;
        
        }
        
}
My latest code
<apex:page standardController="Packages__c" extensions="packageextensions">

<apex:listViews type="Product2" />

<apex:outputText value="{!Products}" />

</apex:page>

 

 

Abhinav GuptaAbhinav Gupta

Why are you printing product list like this

<apex:outputText value="{!Products}" />

You can use apex:repeat to create required markup, or use apex:pageBlocktable or apex:dataTable.