+ Start a Discussion
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in 

How to insert Map key and vales while save action ?

Public Map<Quote_line_item__c,list<batch__c>> blist{ get; set;}
public list<batch__c> ctlist{get; set;}
 Quote_line_item__c acc= new Quote_line_item__c(Master_Id__c='Qli'+count);
         ctlist=new list<batch__c>();
         batch__c ct=new batch__c(Dummy_field__c='BTH'+bcount,Quote_line_item__r=new Quote_line_item__c(Master_Id__c='Qli'+count));       
         ctlist.add(ct);
         blist.put(acc,ctlist);



Master_id__c is a link field between Quote_line_item__c and batch
For Every action I am using this map to put 

At last i want to insert those values in Map blist how can i do that help me ?
 
for(quote_line_item__c q:blist.keyset()){
       For(batch__c b:  ){
     }
     }

How to compete thsi save logic .Help me
Best Answer chosen by d.tejdeep@nicomatic.in
Jigar.LakhaniJigar.Lakhani
Hello,

I assume that you need to insert key of map as parent objects and value of map as child object. So you need to assign parent record id to its child record and which is possible after insert parent records before inserting child records.

Please try with below code. Before execute that code I assume that you have prepared your map with Quote line item and its batch which needs to insert.
Map<Integer,List<batch__c>> mapIndexWithBatch = new Map<Integer,List<Batch__c>();

// Insert quote line items and prepare map of index and batch
List<Quote_line_item__c> listInsertQuoteLineItem = new List<Quote_line_item__c>();
Integer intCount = 0;
for (Quote_line_item__c objQuoteLineItem:blist.KeySet()) {
	listInsertQuoteLineItem.Add(objQuoteLineItem);
	mapIndexWithBatch.put(intCount,blist.Get(objQuoteLineItem));
	intCount++;
}
Insert listInsertQuoteLineItem;

// Prepare list of batch and assign quote line item id based on index
// and insert list of batch
List<Batch__c> listInsertBatch = new List<Batch__c>();
for (Integer intCurrentIndex:mapIndexWithBatch.KeySet()) {
	for (Batch__c objBatch:mapIndexWithBatch.Get(intCurrentIndex)) {
		objBatch.Quote_line_item__c = listInsertQuoteLineItem[intCurrentIndex].Id;
		listInsertBatch.Add(objBatch);
	}
}
Insert listInsertBatch;

Thanks & Cheers,
Jigar (pateljb90@gmail.com)

 

All Answers

Shrikant BagalShrikant Bagal
Please post whole save Method.
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
public class quoteController{
       Public set<Quote_line_item__c> acclist{get ; set;}
       Public string parentname{get ; set;}
       Public string childname{get ; set;}
     Public Map<Quote_line_item__c,list<batch__c>> blist{ get; set;}
     public list<batch__c> ctlist{get; set;}
     integer count;
     Integer bcount;
    
        public quoteController(){
              count=1; 
              bcount=1; 
         acclist=new set<Quote_line_item__c>();      
         blist=new Map<Quote_line_item__c,list<batch__c>>();
         Quote_line_item__c acc= new Quote_line_item__c(Master_Id__c='Qli'+count);
         system.debug(acc.Master_Id__c);
         ctlist=new list<batch__c>();
         batch__c ct=new batch__c(Dummy_field__c='BTH'+bcount,Quote_line_item__r=new Quote_line_item__c(Master_Id__c='Qli'+count));         
         system.debug(ct.Dummy_field__c);
         ctlist.add(ct);
         blist.put(acc,ctlist); 
         acclist.add(acc);
    }

       Public void addparent(){
         count=count+1;
         bcount=bcount+1;
         Quote_line_item__c acc= new Quote_line_item__c(Master_Id__c='Qli'+count);
         ctlist=new list<batch__c>();
         batch__c ct=new batch__c(Dummy_field__c='BTH'+bcount,Quote_line_item__r=new Quote_line_item__c(Master_Id__c='Qli'+count));
         ctlist.add(ct);
         blist.put(acc,ctlist);
         acclist.add(acc);
       }
       Public Void addchild(){
       count=count+1;
       bcount=bcount+1;
       Quote_line_item__c acc= new Quote_line_item__c(Master_Id__c=parentname);
       list<batch__c> ctlist=blist.get(acc);
       batch__c ct=new batch__c(Dummy_field__c='BTH'+bcount,Quote_line_item__r=new Quote_line_item__c(Master_Id__c=parentname));
       ctlist.add(ct);
       blist.put(acc,ctlist);
       } 
       Public void deletechild(){
       list<batch__c> b=new list<batch__c>();
       batch__c ct=new batch__c(Dummy_field__c=childname,Quote_line_item__r=new Quote_line_item__c(Master_Id__c=parentname));
       b.add(ct);
       b.clear();
      }
      Public Void deleteparent(){
      Quote_line_item__c acc= new Quote_line_item__c(Master_Id__c=parentname);
      blist.remove(acc);
     }
     Public pagereference save(){
     for(quote_line_item__c q:blist.keyset()){
       For(batch__c b:  ){
     }
     }
      return null;
      }
  }

visualforce page:
 
<apex:page Controller="quoteController" showHeader="false">
<apex:form >
   <apex:pageBlock title="Bulk Quotelineitem Create" id="wtable">
      <apex:pageBlockTable value="{!Blist}" var="a" >
      <apex:column >

         </apex:column>
         <apex:column headerValue="Parent">
            <apex:inputField value="{!a.Name}"/>
         </apex:column>
         <apex:column headerValue="Name">
            <apex:outputField value="{!a.Master_Id__c}"/>
         </apex:column>
         <apex:column >
         <apex:commandButton value="Delete Parent" action="{!deleteparent}" rerender="wtable">
         <apex:param assignTo="{!parentname}" value="{!a.name}" Name="parentname777"/>
         </apex:commandButton>
         </apex:column>
         <apex:column breakBefore="true" colspan="2">
         <apex:pageblockTable value="{!blist[a]}" var="c" >
         <apex:column headerValue="Contact Name">
           <apex:inputField value="{!c.Name}"/>
         </apex:column>
         <apex:column headerValue="Dummy Field">
          <apex:inputField value="{!c.Dummy_field__c}"/>
         </apex:column>
         <apex:column >
          <apex:commandlink Value="Add child" action="{!addchild}" rerender="wtable">
         <apex:param assignTo="{!parentname}" value="{!a.Master_Id__c}" Name="parentname777"/>
         </apex:commandlink>
         </apex:column>
         <apex:column >
         <apex:commandlink Value="Delete Child" action="{!deletechild}" onclick="remove(this)"  rerender="wtable">
         <apex:param assignTo="{!parentname}" value="{!a.Master_Id__c}" Name="parentname777"/>
         <apex:param assignTo="{!childname}" value="{!c.Dummy_field__c}" Name="parentname777"/>
         </apex:commandlink>
         </apex:column>
         </apex:pageblockTable>
         </apex:column>
         
      </apex:pageBlockTable>
   </apex:pageblock>
   <apex:commandButton Value="Add parent"   action="{!addparent}" rerender="wtable"/>
   <apex:commandButton Value="save"   action="{!save}" rerender="wtable"/>
 </apex:form>
</apex:page>

 
Shrikant BagalShrikant Bagal
I did not understand your logic buddy, could you please describe it.
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
Consider two objects  parent and child 

In my visualforce page,I have a buttons ike "Add parent" "Add child" .

Initially when i click on add parent it will show me the layout of inserting one parent,one child 

when i clik on add child i will add child in layout bychecking field master_id__c in parent it will add 

For every add buttons "I am updating this map using put " Public Map<Quote_line_item__c,list<batch__c>> blist{ get; set;}

Every thing done at the UI only and last i want to insert this Map Blist into databse 
Shrikant BagalShrikant Bagal
Please try following save code:
 
Public pagereference save(){
List<quote_line_item__c> lstParent = new List<quote_line_item__c>();
List<batch__c> lstChild = new List<quote_line_item__c>();
     for(quote_line_item__c q:blist.keyset()){
		lstParent.add(q);
		lstChild.addAll(blist.get(q));
     }
  Database.upsert(lstParent);
  Database.upsert(lstChild);
      return null;
      }

 
Jigar.LakhaniJigar.Lakhani
Hello,

I assume that you need to insert key of map as parent objects and value of map as child object. So you need to assign parent record id to its child record and which is possible after insert parent records before inserting child records.

Please try with below code. Before execute that code I assume that you have prepared your map with Quote line item and its batch which needs to insert.
Map<Integer,List<batch__c>> mapIndexWithBatch = new Map<Integer,List<Batch__c>();

// Insert quote line items and prepare map of index and batch
List<Quote_line_item__c> listInsertQuoteLineItem = new List<Quote_line_item__c>();
Integer intCount = 0;
for (Quote_line_item__c objQuoteLineItem:blist.KeySet()) {
	listInsertQuoteLineItem.Add(objQuoteLineItem);
	mapIndexWithBatch.put(intCount,blist.Get(objQuoteLineItem));
	intCount++;
}
Insert listInsertQuoteLineItem;

// Prepare list of batch and assign quote line item id based on index
// and insert list of batch
List<Batch__c> listInsertBatch = new List<Batch__c>();
for (Integer intCurrentIndex:mapIndexWithBatch.KeySet()) {
	for (Batch__c objBatch:mapIndexWithBatch.Get(intCurrentIndex)) {
		objBatch.Quote_line_item__c = listInsertQuoteLineItem[intCurrentIndex].Id;
		listInsertBatch.Add(objBatch);
	}
}
Insert listInsertBatch;

Thanks & Cheers,
Jigar (pateljb90@gmail.com)

 
This was selected as the best answer