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.
Best Answer
You can theoretically do it as follows:
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.