0

I think now you can able to understand I am creating a page that i have to insert multiple parent and multiple child in single insert call in single page . Visualforce page:

<apex:page controller="ManageListController" showHeader="false">
 <apex:form >
   <apex:pageBlock title="Bulk Quotelineitem Create" id="wtable">
      <apex:pageBlockTable value="{!colist}" var="a" >
      <apex:column >
          <apex:commandlink Value="Add child" action="{!addchild}" rerender="wtable">
         <apex:param assignTo="{!parentname}" value="{!a.name}" Name="parentname777"/>
         </apex:commandlink>
         </apex:column>
         <apex:column headerValue="Parent">
            <apex:inputField value="{!a.Phone}"/>
         </apex:column>
         <apex:column headerValue="Name">
            <apex:outputField value="{!a.Name}"/>
         </apex:column>
         <apex:column >
         <apex:commandButton value="Delete Account" action="{!deleteaccount}" rerender="wtable">
         <apex:param assignTo="{!parentname}" value="{!a.name}" Name="parentname777"/>
         </apex:commandButton>
         </apex:column>
         <apex:column breakBefore="true" colspan="1">
         <apex:pageblockTable value="{!colist[a]}" var="c" >
         <apex:column headerValue="Contact Name">
           <apex:inputField value="{!c.name}"/>
         </apex:column>
         <apex:column headerValue="Account id">
          <apex:inputField value="{!c.Accountid}"/>
         </apex:column>
         <apex:column >
          <apex:commandlink Value="Add contact" action="{!addcontact}" rerender="wtable">
         <apex:param assignTo="{!parentname}" value="{!a.name}" Name="parentname777"/>
         </apex:commandlink>
         </apex:column>
         <apex:column >
         <apex:commandButton Value="Delete contact" action="{!deletecontact}" >

         </apex:commandbutton>
         </apex:column>
         </apex:pageblockTable>
         </apex:column>

      </apex:pageBlockTable>
   </apex:pageblock>
   <apex:commandButton Value="Add Account"   action="{!addaccount}" rerender="wtable"/>
 </apex:form>   
</apex:page>

Controller :

public class ManageListController{
       Public set<account> acclist{get ; set;}
       Public string parentname{get ; set;}
       Public string childname{get ; set;}
     Public Map<Account,list<contact>> colist{ get; set;}
     public list<contact> ctlist{get; set;}
     integer count;

          public ManageListController(){
         count=1;   
         acclist=new set<account>();      
         colist=new Map<Account,list<contact>>();
         Account acc= new account(name='acc'+count);
         ctlist=new list<contact>();
         contact ct=new contact(Lastname='Karthika');

         ctlist.add(ct);
         colist.put(acc,ctlist); 
         acclist.add(acc);
         }

       Public void addaccount(){
         count = count+1;
         Account acc= new account(Name='acc'+count);
         ctlist=new list<contact>();
         contact ct=new contact(Lastname='Karthika');
         ctlist.add(ct);
         colist.put(acc,ctlist);
         acclist.add(acc);
       }
       Public Void addcontact(){
       Account acc= new account(name=parentname);
       list<contact> ctlist=colist.get(acc);
       contact ct=new contact(Lastname='Karthika');
       ctlist.add(ct);
       colist.put(acc,ctlist);
       } 
       Public void deletecontact(){

      }
      Public Void deleteaccount(){
      Account acc= new account(name=parentname);
      colist.remove(acc);
     }
     Public pagereference save(){
     id acid;
     Map<account,id> Mapaccountid= new Map<account,id>();
     set<account> accl=colist.keyset();
     list<account> acli=new list<account>();
     for(account a:accl){
     acli.add(a);
     Mapaccountid.put(a,a.id);
     }
     insert acli;

     list<contact> colis=new list<contact>();

     For(Account acc:accl){
      For(contact col:ctlist){
      acid=Mapaccountid.get(acc);
      contact co=new contact();
      co.lastname='france';
      co.accountid=acid;
     colis.add(co);
     }}
     insert colis;
     return null;
      }
}

I Cant able to put uniqueness for deleting a child record .I dont know how i cant relate parent to child while inserting .

Thanks in advance

2

Your problem is most likely that you are not clear about your datamodel. Reading your code I was not able to figure exactly how the ERD looks like an what you want to achieve. So it's unclear what you are asking exactly.

But conceptually this basics might help you:

  • draw your ERD (entity relationship diagram)
  • mind that there are only 1:n relationships natively
  • an m:n relationship needs a junction object
  • Lookup- and MasterDetail relationships work always from child to parent (=master)

Based on this you might rethink your model and maybe rephrase your question.

What I noticed in your code that can't work:

  • I can't see a query to populate your pageblocktable
  • your action functions have no DML to store anything
  • your list consists of contacts, addparent and addchild deal with accounts and the pageblocktitle says 'Bulk Quotelineitem Create' - how that can fit together?

To learn more about m:n relationships and junctions object I would recommend that you search for it. You need at least 3 objects: the two you wand to relate and the third to glue them together (this is the junction object). At the junction object you need a relation to both other objects to make it work. It's like Mathew Lamb said here:

A many-to-many relationship consists of three objects: Object A and Object B are the two primary objects, and Object J is the junction that enables the many to many.

Object A <--M/D-- Object J --M/D--> Object B

So on Object J you need to create two Master-Detail relationships, one that points to Object A and one that points to Object B.

There are also a couple of other useful links:

In general I would also reconsider your UI. It doesn't look very usable to me. But first get clear on your model. I could suggest you something but therefore you need to tell a bit more what you are going to achieve. The exact requirement. Verbosely.

  • now you understood what i am trying to do – Karthika Ram Jun 29 '15 at 12:01
  • I have a problem with delete child button and in regarding button i cant able to relate account and contact .it is saving as two different records – Karthika Ram Jun 29 '15 at 12:03
  • to be honest: no, I still don't figure it without very long thinking. Take a few words and tell us your story in plain english, please. The screenshot looks not like anything I have seen before. Nothing familiar, Nothing standard, very exotic and unconventional, at least for me... – Uwe Heim Jun 29 '15 at 12:05
  • K just my logic is to insert multiple parent and its related multiple child in vf page – Karthika Ram Jun 29 '15 at 12:07
  • to relate account and contact there is only ONE standard field called Contact.AccountId - now do you want to use that? If so you have to set it. But there is only ONE Account per contact. If you need more, you NEED a junction object. It's all about the model first. Are you aware of that? No direct m:n. – Uwe Heim Jun 29 '15 at 12:10

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.