I have a visualforce page that contains the following snippet:
<apex:pageBlockTable id="qliList" value="{!qliProdList}" var="qliProd" columns="3">
<apex:column >
<apex:facet name="header">Name</apex:facet>
{!qliProd.Name}
</apex:column>
<apex:column >
<apex:facet name="header">Quantity</apex:facet>
<apex:outputText value="{!qliQuantity}">
<apex:param name="qliID" value="{!qliProd.Id}"/>
</apex:outputText>
</apex:column>
<apex:column >
<apex:facet name="header">Action</apex:facet>
<apex:commandButton action="{!delQLI}" rerender="qliList" value="Remove">
<apex:param name="qliID" value="{!qliProd.Id}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
I have the following relevant apex code:
public class MyController {
public List<QuoteLineItem> qliList = new List<QuoteLineItem>();
public MyController() {
// some code that instantiates (but does not insert) quote line items
// and adds them to qliList. Code is omitted for brevity
}
public List<Product2> get QliProdList() {
List<Product2> qliProdList = new List<Product2>();
for (QuoteLineItem qli : qliList) {
Product2 prod = [SELECT Id, Name FROM Product2 WHERE Id = :qli.Product2Id];
qliProdList.add(prod);
}
return qliProdList;
}
public Double getQLIQuantity() {
String qliID = ApexPages.CurrentPage().getParameters().get('qliID');
system.debug('trying to get QLI quantity for ID: ' + qliID);
for (QuoteLineItem qli : qliList) {
if (qli.Product2Id == qliId) {
return qli.Quantity;
}
}
return null;
}
public void delQLI() {
String qliID = ApexPages.CurrentPage().getParameters().get('qliID');
for (Integer i = qliList.size() - 1; i >= 0; i--) {
if (qliList[i].Product2Id == qliID) {
qliList.remove(i);
}
}
return;
}
}
The "remove" commandButton works beautifully. However, I can't get the quote line item's quantity value in the apex:outPutText. qliID comes back null. I did some googling and apparently people are saying that I need to make a wrapper class for my qliProdList. However, I tried all the examples I found on stack overflow and none of them work. I managed to make a qliProdListWrapper class and added a "public List qliProdList {get; set;}" that doesn't cause the entire vf page to stop working, but then my list just comes back empty.
In a nutshell, all I want is to be able to generate a list using apex:pageBlockTable of my quote line items where I can display the product name, quote line item quantity and have a commandbutton to remove them from the list. I've been pulling my hair out trying to figure out a way to make this work since the quote line item doesn't store the product's name - I have to fetch the product2 object instance for that using the quote line item's Product2Id field which is a major PITA, but I don't know any other way to get this data.
Please let me know if there's a clean way I can do this
P.S. the quote line items have NOT been inserted yet. This VF page is supposed to display a preview of them before insertion. They only exist as instances of QuoteLineItem(); in my apex controller and have not been inserted yet. There is no way to pull them up with a SOQL query since they would have to be inserted first for that to work.
So again, all I want is an apex:pageBlockTable that displays all the quote line items in my qliList, but I also need it to display product names, which are fields in the related Product2 object, not the quote line item object (hence why I had to write all that extra ugly code to generate qliProdList from the quote line item's product2Id).
Worst case scenario, I could create a formula field in the Quote Line Item object itself that gets the name from the associated Product2 record, but I'd rather do it in the apex controller if possible