You need to sign in to do that
Don't have an account?
Parent record not saving - related line item records on a parent record with multi add button
Hello
I am trying to achieve having this parent record (miiFinance__Invoice__c) save. My code is not quite finished, however this is my first hurdle. I reused this code when creating multiple account records from the one page, so thats why some reference to acct is in there.
My outcome is to have this invoice__c record save and have the related miiFinance__Invoice_Line_Item__c records relate and save also. This code works if i manually look up and already saved invoice. How can i
1. pass the future/new ID of ths miiFinance__Invoice__c record into the newly, to be created, miiFinance__Invoice_Line_Item__c records?
2. have the parent miiFinance__Invoice__c record save to the database upon saving the record?
Thank you in advance
Here is my VF page
<apex:page StandardController="Invoice__c" extensions="MultiAdd" id="thePage"> <apex:form > <apex:pageblock id="pb" > <apex:pageBlockButtons > <apex:commandbutton value="Add Line Item" action="{!Add}" rerender="pb1"/> <apex:commandbutton value="Save Invoice" action="{!Save}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Information" columns="2" collapsible="false"> <apex:inputField value="{!Invoice__c.Customer__c}" required="true" /> <apex:inputField value="{!Invoice__c.Currency__c}" required="true" /> <apex:inputField value="{!Invoice__c.Date_Document__c}" required="true" /> <apex:inputField value="{!Invoice__c.Type__c}" required="true" /> <apex:inputField value="{!Invoice__c.Due_Date__c}" required="true" /> </apex:pageBlockSection> <apex:pageblock id="pb1"> <apex:repeat value="{!lstInner}" var="e1" id="therepeat"> <apex:panelGrid columns="8"> <apex:panelGrid headerClass="Name"> <apex:facet name="header">Delete</apex:facet> <apex:commandButton value="Remove" action="{!Del}" rerender="pb1"> <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param> </apex:commandButton> </apex:panelGrid> <apex:panelGrid title="SPD" > <apex:facet name="header">Display Name</apex:facet> <apex:inputfield value="{!e1.acct.Display_Name__c}" required="true"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Product</apex:facet> <apex:inputfield value="{!e1.acct.Product_For_Sale__c}" required="true"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Currency</apex:facet> <apex:inputfield value="{!e1.acct.Currency__c}" required="true"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Tax Type</apex:facet> <apex:inputfield value="{!e1.acct.Tax__c}" required="true"/> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Quantity</apex:facet> <apex:inputfield value="{!e1.acct.Amount_Inc_Tax__c}" required="true" /> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Invoice</apex:facet> <apex:inputfield value="{!e1.acct.Invoice__c}" required="true" /> </apex:panelGrid> <apex:panelGrid > <apex:facet name="header">Amount</apex:facet> <apex:inputfield value="{!e1.acct.Amount_Inc_Tax__c}" required="true" /> </apex:panelGrid> </apex:panelgrid> </apex:repeat> </apex:pageBlock> </apex:pageblock> </apex:form> </apex:page>
Here is my extension
public class MultiAdd { //will hold the account records to be saved public List<miiFinance__Invoice_Line_Item__c>lstAcct = new List<miiFinance__Invoice_Line_Item__c>(); //list of the inner class public List<innerClass> lstInner { get;set; } //will indicate the row to be deleted public String selectedRowIndex {get;set;} //no. of rows added/records in the inner class list public Integer count = 1; //{get;set;} ////save the records by adding the elements in the inner class list to lstAcct,return to the same page public PageReference Save() { PageReference pr = new PageReference('/apex/MultiAdd'); for(Integer j = 0;j<lstInner.size();j++) { lstAcct.add(lstInner[j].acct); } insert lstAcct; pr.setRedirect(True); return pr; } //add one more row public void Add() { count = count+1; addMore(); } /*Begin addMore*/ public void addMore() { //call to the iner class constructor innerClass objInnerClass = new innerClass(count); //add the record to the inner class list lstInner.add(objInnerClass); system.debug('lstInner---->'+lstInner); }/* end addMore*/ /* begin delete */ public void Del() { system.debug('selected row index---->'+selectedRowIndex); lstInner.remove(Integer.valueOf(selectedRowIndex)-1); count = count - 1; }/*End del*/ /*Constructor*/ public MultiAdd(ApexPages.StandardController ctlr) { lstInner = new List<innerClass>(); addMore(); selectedRowIndex = '0'; }/*End Constructor*/ /*Inner Class*/ public class innerClass { /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */ public String recCount {get;set;} public miiFinance__Invoice_Line_Item__c acct {get;set;} /*Inner Class Constructor*/ public innerClass(Integer intCount) { recCount = String.valueOf(intCount); /*create a new account*/ acct = new miiFinance__Invoice_Line_Item__c(); }/*End Inner class Constructor*/ }/*End inner Class*/ }/*End Class*/
As you are using the standard controller, you can access the new Invoice__c item via the getRecord() method. If you then insert this, the record will be updated to contain the id. You can then use this to populate the relationship before saving the child records.
Hi Bob
Thanks for your prompt reply
I see you are quite active in the community
Would you have a link to another post perhaps, or some code which would help me further on this. I am new.
Would you mind posting a simple example of amendment to the current code which would allow me to use it further?
Thanks in advance
Dave
Hi,
It should be pretty straightforward. Save a reference to the record in your constructor:
then in your save, insert the new record and update the children before saving:
thank you for the help, its put me in the right direction. i am getting an error from your first part
Error: MultiAdd Compile Error: Variable does not exist: Invoice__c at line 70 column 14
the error line is in blue
That's missing the controller variable: