0

I'm creating a page wherein I can create multiple order items (Order__c) for every order form (Order_Form__c) through "New Order Items" button in the Order__c related list. But every time I save, there's this error: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Order Form]: [Order Form].

Here's the controller:

public class NewOrderController {

    private ApexPages.StandardController orderController;
    public Order__c order {get;set;}
    public Order_Form__c orderform {get;set;}
    public String ordfName {get;set;}
    public List<OrderWrapper> orderlist {get;set;} 
    public Integer rowToRemove {get;set;}
    public String acctname {get; set;} 
    Public Id orderformID {get; set;} 

    public NewOrderController(ApexPages.StandardController controller) {
        orderController = controller;
        this.order = (Order__c)controller.getRecord();
        mobile = FALSE;
        orderlist = new List<OrderWrapper>();
        addNewRowToOrdList();



    }
    public void addNewRowToOrdList() {
        List<OrderWrapper> temp_orderlist = new List<OrderWrapper>();
        OrderWrapper newRecord = new OrderWrapper();
        Order__c newOrderRecord = new Order__c();
        newRecord.ord = newOrderRecord;
        newRecord.index = temp_orderlist.size();
        temp_orderlist.add(newRecord);
        for(OrderWrapper ewloop : temp_orderlist) {
            orderlist.add(ewloop);
        }
    }

    public void removeRowFromOrdList() {
        List<OrderWrapper> rem_orderlist = new List<OrderWrapper>();
        for(OrderWrapper ewloop : orderlist) {
            rem_orderlist.add(ewloop);
        }
        rem_orderlist.remove(rowToRemove);
        orderlist.clear();
        for(OrderWrapper reloop : rem_orderlist) {
            orderlist.add(reloop);
        }
    }

    public void save() {

        List<OrderWrapper> sv_orderlist = new List<OrderWrapper>();
        List<Order__c > newOrderList = new List<Order__c >();
        Order_Form__c orderform = new Order_Form__c();
        Id orderformId;
        Decimal total = 0;
        for(OrderWrapper ewloop : orderlist) {
            sv_orderlist.add(ewloop);
        }

        if(sv_orderlist !=null && !sv_orderlist.isEmpty()) {

            for(OrderWrapper eachRecord : sv_orderlist) {
                Order__c ordTemp = new Order__c ();
                ordTemp.Total_Price__c = eachRecord.ord.Total_Price__c;
                ordTemp.Product__c = eachRecord.ord.Product__c;
                ordTemp.Quantity__c = eachRecord.ord.Quantity__c;
                ordTemp.Order_Form2__c = orderform.id;
                newOrderList.add(ordTemp);

            }
        }

        if(newOrderList.size() > 0) {
            insert newOrderList;
        }

    }

    public class OrderWrapper {

        public Integer index {get;set;}
        public Order__c ord {get;set;}

    }

}

And here's vf page:

<apex:page standardController="Order__c" extensions="NewOrderController">
    <apex:sectionHeader title="Order" subtitle="New Order"/>

    <apex:form >
        <apex:pageMessages /> 
        <apex:pageBlock title="Order" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/> 
            </apex:pageBlockButtons>

            <apex:pageBlockSection columns="1" title="Order Information">
                <apex:inputField value="{!order.Order_Form2__c}"/>
            </apex:pageBlockSection>

            <apex:pageBlockSection title="Product Information">
                <apex:outputPanel id="tablepanel">
                <apex:variable value="{!0}" var="rowNum"/>
                    <apex:pageBlockTable value="{!orderlist}" var="o">

                        <apex:column headerValue="Action" style="width:80px">
                            <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromOrdList}" rendered="{!rowNum > 0}" rerender="tablepanel" immediate="true" > 
                                <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
                            </apex:commandLink>
                            <apex:variable var="rowNum" value="{!rowNum + 1}"/>
                        </apex:column>

                        <apex:column headerValue="Product Name" >
                            <apex:inputField value="{!o.ord.Product__c}" required="true"/>
                        </apex:column>  

                        <apex:column headerValue="Quantity" >
                            <apex:inputField value="{!o.ord.Quantity__c}" required="true"/>
                        </apex:column>

                    </apex:pageBlockTable>

                <apex:commandButton value="Add More" action="{!addNewRowToOrdList}" rerender="tablepanel"/>
                </apex:outputPanel>  
            </apex:pageBlockSection>

        </apex:pageBlock>

    </apex:form> 

</apex:page>
0

There is an issue with line ordTemp.Order_Form2__c = orderform.id in your controller's save method. You have to assign correct parent object Id. Currently you are assigning blank Id that's why it is throwing "REQUIRED_FIELD_MISSING" exception.

  • How can I assign the correct parent object Id? I assigned orderform as the parent object, so I thought creating a reference to the master is enough – curdual Aug 7 '16 at 9:35
  • I think you should pass parent object Id through this.order.Order_Form2__c. So your statement would be ordTemp.Order_Form2__c = this.Order_Form2__c. Let me know if you still facing issue. – Pragati Jain Aug 7 '16 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.