0

How can I add multiple products to order__c form using only one insert statement? Wherein one would not rely to order line item to just create products. I have three custom objects: Order__c, Order_LineItem__c and Product__c. Any help would be great!

OrderFormCX

public class OrderFormCX{
    ApexPages.standardController ordercontroller;
    public Order_Form__c order {get;set;}
    public Order__c orderlineitem {get;set;}
    Public List<orderWrapper> orderWrapperList {get;set;} 
    Public Integer rowToRemove {get;set;}
    Public List<String> orderId = new List<String>();
    Public List<String> mobileOrderId = new List<String>();


    public OrderFormCX(ApexPages.StandardController controller){
        ordercontroller = controller;
        order = new Order_Form__c();
        orderlineitem = new Order__c();
        orderWrapperList = new List<orderWrapper>();
        addNewRowToExpList();

    }

    public PageReference saveTo(){

        Order_Form__c order2 = new Order_Form__c();
        order2.Account__c = order.Account__c;
        order2.Name = order.Name;
        order2.Purchased_Date__c = order.Purchased_Date__c;
        order2.Delivery_Date__c = order.Delivery_Date__c;
        order2.Bearer__c = order.Bearer__c;
        order2.Notes__c = order.Notes__c;    
        insert order2;


        Pagereference pageref = new Pagereference('/' + order2.Id);
        pageref.setRedirect(true);
        return pageref; 

    }

    public PageReference doCancel(){
        return ordercontroller.cancel();
    }

    public void addNewRowToExpList() {  

        List<orderWrapper> tempOrderWrapperList = new List<orderWrapper>();
        orderWrapper newRecord = new orderWrapper();
        Order__c newOrderItemRecord = new Order__c();
        newRecord.ord = newOrderItemRecord;
        newRecord.index = tempOrderWrapperList.size();
        tempOrderWrapperList.add(newRecord);


        for(orderWrapper ow : tempOrderWrapperList) {

            ow.ord.Discount__c = 0;
            ow.ord.Quantity__c = 0;
            orderWrapperList.add(ow);
        }
    }

     public void removeRowFromExpList() {

        List<orderWrapper> remOrderWrapperList = new List<orderWrapper>();
        for(orderWrapper ow : orderWrapperList) {
            remOrderWrapperList.add(ow);
        }
        remOrderWrapperList.remove(rowToRemove);
        orderWrapperList.clear();

        for(orderWrapper row : remOrderWrapperList) {
            orderWrapperList.add(row);
        }
    }


    public class orderWrapper{
        public Integer index {get; set;}
        public Order__c ord {get; set;}
        public Order_Form__c ordform {get; set;}
    }


}

OrderFormPage

<apex:page standardController="Order_Form__c" extensions="OrderFormCX">
   <apex:sectionHeader title="Order Form Edit" subtitle="New Order Form"/>
      <apex:form >
         <apex:pageBlock title="Order Form Edit" mode="edit">
            <apex:pageBlockButtons >
               <apex:commandButton value="Save" action="{!Saveto}"/>
               <apex:commandButton value="Cancel" action="{!doCancel}"/> 
            </apex:pageBlockButtons>

            <apex:pageBlockSection columns="2" title="Order Information">
               <apex:inputField value="{!order.Name}"/>
               <apex:inputField value="{!order.Purchased_Date__c}"/>
               <apex:inputField value="{!order.Account__c}"/>

            </apex:pageBlockSection>

            <apex:pageBlockSection title="Delivery Information">

               <apex:inputField value="{!order.Delivery_Date__c}"/>
               <apex:inputField value="{!order.Bearer__c}"/>
               <apex:inputField value="{!order.Notes__c}"/>

            </apex:pageBlockSection>

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

                  <apex:column headerValue="Action" style="width:80px">
                     <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromExpList}" 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 Category" >
                     <apex:inputField value="{!e.ord.Product_Category__c}" required="true"/>
                  </apex:column> 

                  <apex:column headerValue="Size" >
                     <apex:inputField value="{!e.ord.Size__c}" required="true"/>
                  </apex:column>

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

                  <apex:column headerValue="Outlet" >
                     <apex:inputField value="{!e.ord.Outlet_del__c}" required="true"/>
                  </apex:column>

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

                  <apex:column headerValue="Discount" >
                     <apex:inputField value="{!e.ord.Discount__c}" required="true"/>
                  </apex:column>

               </apex:pageBlockTable>

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

         </apex:pageBlock>

      </apex:form> 

</apex:page>

The fields from Order_Form__c are successfully saved. My only problem is that fields coming from the Order__c object are not saved.

My agenda is to save both of them at the same time, wherein upon saving, I can see details not only on the Order_Form__c but also on the Order__c related list.

  • 2
    Where's the rest of your code to show us what you're trying to do? – crmprogdev Jun 15 '16 at 16:41
  • @crmprogdev please see my edited post, I hope it helps – asdfg Jun 15 '16 at 17:14
3

You can theoretically do it as follows:

SObject[] records = new SObject[0];
records.add(new Order__c(External_Id__c='1234', Name='ABC', ...));
for(Order_LineItem__c lines: orderLines) {
  lines.Order__r = new Order__c(External_Id__c='1234');
}
records.addAll(orderLines);
insert records;

By setting the relationship to a unique external ID, salesforce is smart enough to figure out that you want the records to be related together.

You must make sure that (a) External_Id__c is set as an External ID, and (b) the value is guaranteed to be unique in the system.


Edit: In response to the edit with code, I've included an example of how this might be done. You were not actually saving your line items initially, so some field names are missing, and this might require further work.

public PageReference saveTo() {
    SObject[] records = new SObject[] { order };
    // Just needs to be unique, so this should do it.
    order.External_Id__c = ''+Crypto.getRandomLong();
    for(orderWrapper wrapper: orderWrapperList) {
        wrapper.ord.Order__r = new Order__c(External_Id__c=order.External_Id__c);
        records.add(wrapper.ord);
    }
    insert records;
    return new ApexPages.StandardController(order).view();
}
| improve this answer | |
  • I'm using wrapper class, and I'm not sure how to apply this kind of approach in my code. – asdfg Jun 15 '16 at 17:24
  • Hi sorry for asking again, but upon saving the order record, it displayed an error: DML statement found null SObject at position 1 Error is in expression '{!Saveto}' in component <apex:commandButton> in page orderformpage: Class.OrderFormCX.saveTo: line 28, column 1 – asdfg Jun 15 '16 at 18:57
  • ' public PageReference saveTo() { Order_Form__c[] records = new Order_Form__c[] { order }; order.External_Id__c = ''+Crypto.getRandomLong(); for(orderWrapper wrapper: orderWrapperList) { wrapper.ord.Order_Form__r = new Order_Form__c(External_Id__c=order.External_Id__c); records.add(wrapper.ordform); } insert records; return new ApexPages.StandardController(order).view(); }' – asdfg Jun 15 '16 at 18:58
  • I already created an external id, with text datatype, called External_Id__c on the parent object, Order_Form__c, but there's an error – asdfg Jun 15 '16 at 18:59
-1

Taken help from @sfdcFox and please change your code like this, it should work. Foreign key reference has been corrected. Instead of Order__r, it should be Order_Form__r.

public PageReference saveTo() 
{
    SObject[] records = new SObject[] { order };
    // Just needs to be unique, so this should do it.
    order.External_Id__c = ''+Crypto.getRandomLong();
    for(orderWrapper wrapper: orderWrapperList) 
    {
        Order_Form__c orderFormObj = new Order_Form__c(External_Id__c=order.External_Id__c);
        wrapper.ord.Order_Form__r = orderFormObj;
        records.add(wrapper.ord);
    }
    insert records;
    return new ApexPages.StandardController(order).view();
}

Also refer Creating Parent and Child Records in a Single Statement Using Foreign Keys for more information.

I have tested at my developer org and it is working correctly. Thanks to @sdfcFox for a good pointer.

| improve this answer | |

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.