0

I have added a standardcontroller ("OrderItem) to my visualforce page and also an extension. I am trying to view the OrderItem details based on the Order record ID of the page. But I keep getting the error "Id value is not valid for the OrderItem standard controller".

Because I want to override the "EditProducts" button in "Order Products" with my visualforce page. I must use a standardController for "OrderItem". Which is the API name for "Order Products"

Please help. Thank you!

<apex:page standardController="OrderItem" extensions="OrderProductExtension" lightningStylesheets="true">
<apex:form>
    <apex:pageBlock id="products_list" title="Order Products">
        
        
        <apex:pageBlockTable value="{! Products}" var="Oi" >
            <apex:column value="{! Oi.PricebookEntry.Product2.Name}">
                <apex:facet name="header">
                    <apex:commandLink action="{! sortByName}" reRender="products_list">
                        <apex:outputText value="{! $ObjectType.OrderItem.Fields.Product2Id.Label}"/>
                    </apex:commandLink>
                </apex:facet>
            </apex:column>
            <apex:column value="{! Oi.Quantity}">
                <apex:facet name="header">
                    <apex:commandLink action="{! sortByQuantity}" reRender="products_list">
                        <apex:outputText value="{! $ObjectType.OrderItem.Fields.Quantity.Label}"/>
                    </apex:commandLink>
                </apex:facet>
            </apex:column>
            <apex:column value="{! Oi.UnitPrice}">
                <apex:facet name="header">
                    <apex:commandLink action="{! sortByUnitPrice}" reRender="products_list">
                        <apex:outputText value="{! $ObjectType.OrderItem.Fields.UnitPrice.Label}"/>
                   </apex:commandLink>
                </apex:facet>
            </apex:column>
            <apex:column value="{! Oi.TotalPrice}">
                <apex:facet name="header">
                    <apex:commandLink action="{! sortByTotalPrice}" reRender="products_list">
                        <apex:outputText value="{! $ObjectType.OrderItem.Fields.TotalPrice.Label}"/>
                    </apex:commandLink>
                </apex:facet>
            </apex:column>
        </apex:pageBlockTable> 

    </apex:pageBlock>
</apex:form>

</apex:page>

my custom extension:

public class OrderProductExtension{ 

private String sortOrder = 'TotalPrice';
private String currentId;

public OrderProductExtension(ApexPages.StandardController stdController){
    this.currentId = stdController.getId();
}

public List<OrderItem>  getProducts(){

    List<OrderItem> products = Database.query('SELECT Quantity, PricebookEntry.Product2.Name, UnitPrice, TotalPrice FROM OrderItem WHERE' +
    ' orderId =: currentId ORDER BY ' + sortOrder + ' ASC');

    return products;
}

public void sortByName(){
    this.sortOrder = 'PricebookEntry.Product2.Name';
}

public void sortByQuantity(){
    this.sortOrder = 'Quantity';
}

public void sortByUnitPrice(){
    this.sortOrder = 'UnitPrice';
}

public void sortbyTotalPrice(){
    this.sortOrder = 'TotalPrice';
}

}

OPM_OK
  • 190
  • 1
  • 1
  • 11

1 Answers1

1

You will need to cheat a bit. Option 1 is to make the button on Order, not Order Items. It'll display in wrong place but you should be able to hide old button and train users. Then the order id will be passed and it'll work OK. the standardController="Order" will be needed though.

Option 2 is bit messy. something like <apex:page standardController="OrderItem" recordSetVar="records" would indicate that you plan to use this page for multiple items, not just one. Maybe you heard about StandardSetController? But then you don't have the order id (well, if there are existing lines you can do it but what if there are none added yet). See if you can make the button override as type URL (/apex/MyPageName?id={!Order.Id}), not as type button.

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Thank you for the answer. I will look into option 1. Also, "OrderItems" I believe do not have standard lists, so using recordSetVar will give an error on VF page. – OPM_OK Aug 17 '20 at 20:26