function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NikunjVadiNikunjVadi 

Creating multiple child record under parent object

Hey , i am creating mutiple record at once in my custom object. but that object is child object of  FirstVisit(custom object). Here i am not able to insert the record as child record becasue it is not getting parent id when i insert the record. I modified below code to work with my object , but can any one help me how can i give parent id when i am inserting. I know its easy but i am newbie.
public class ManageListController
{
public List<AccountWrapper> wrappers {get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
private Integer nextIdent=0;
 
public ManageListController()
{
  wrappers=new List<AccountWrapper>();
  for (Integer idx=0; idx<5; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
}
 
public void delWrapper()
{
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
  
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
}
 
public void addRows()
{
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
}
 
public PageReference save()
{
  List<Account> accs=new List<Account>();
  for (AccountWrapper wrap : wrappers)
  {
   accs.add(wrap.acc);
  }
  
  insert accs;
  
  return new PageReference('/' + Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix() + '/o');
}
 
public class AccountWrapper
{
  public Account acc {get; private set;}
  public Integer ident {get; private set;}
  
  public AccountWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new Account(Name='Bulk Acc ' + ident);
  }
}
}

Thanks
Best Answer chosen by NikunjVadi
NikunjVadiNikunjVadi
I just needed to pass the parent object id in the mapping field:

public ToDolistWrpper(Integer inIdent)
  {
   ident=inIdent;
 
   tdl=new To_Do_list__c(Name='Work To Do  ' + ident, ObjectMap__c= pageid);

All Answers

bob_buzzardbob_buzzard
That code looks rather familiar - from my managing lists of records blog post (http://bobbuzzard.blogspot.co.uk/2011/07/managing-list-of-new-records-in.html) if I'm not mistaken.

It doesn't look to me like you've changed the code at all - there's no mention of a FirstVisit object for example, its still using accounts.

If each of the accounts needs to look up to a First Visit object, I suggest you pass the ID of the object on the URL and cache that until its time to save in the controller.  Assuming the object id is passed as the parameter named 'FirstVisitId' :

private Id firstVisitId;

public ManageListController()
{
  firstVisitId=ApexPages.CurrentPage().getParameters().get('FirstVisitId);
  wrappers=new List<AccountWrapper>();
  for (Integer idx=0; idx<5; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
}
public PageReference save()
{
  List<Account> accs=new List<Account>();
  for (AccountWrapper wrap : wrappers)
  {
   wrap.acc.First_Visit__c=firstVisitId;
   accs.add(wrap.acc);
  }
  
  insert accs;
  
  return new PageReference('/' + Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix() + '/o');
}



NikunjVadiNikunjVadi
Thanks for the reply, yea i used that controller only  and renamed component only . :p
i am having error assiging parent id.

This is my modified controller;



public class ManageListController
{
public List<ToDolistWrpper> wrappers {get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
private Integer nextIdent=0;
private Id firstVisitId;

public ManageListController()
{
  firstVisitId=ApexPages.CurrentPage().getParameters().get('FirstVisitId');
  wrappers=new List<ToDolistWrpper>();
  for (Integer idx=0; idx<2; idx++)
  {
   wrappers.add(new ToDolistWrpper(nextIdent++));
  }
}
public void delWrapper()
{
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
  
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
}
 
public void addRows()
{
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new ToDolistWrpper(nextIdent++));
  }
}
 
public PageReference save()
{

  List<To_Do_list__c> tdls=new List<To_Do_list__c>();
  for (ToDolistWrpper wrap : wrappers)
  {
  // wrap.tdl=firstVisitId;
  tdls.add(wrap.tdl);
  }
  
  insert tdls;
  Id pageid = ApexPages.currentPage().getParameters().get('id');
  return new PageReference('/apex/Current_Services?id='+pageid);

}
 
public class ToDolistWrpper
{
  public To_Do_list__c tdl {get; private set;}
  public Integer ident {get; private set;}
// public First_Visit__c tdl{get;private set;}
  
  public ToDolistWrpper(Integer inIdent)
  {
   ident=inIdent;
  
   tdl=new To_Do_list__c(Name='Work To Do  ' + ident);
  
  }
}
}


Here First_visit__c is main object. To_Do_list__c is child object. you can get paret id from current page easily. but how to pass this id to to_do_list__c (child object). because without id of parent object , i am getting error if without parent id .

In the solution you gave:
<strong>wrap.acc.First_Visit__c=firstVisitId;</strong>

this is giving error .
NikunjVadiNikunjVadi
I just needed to pass the parent object id in the mapping field:

public ToDolistWrpper(Integer inIdent)
  {
   ident=inIdent;
 
   tdl=new To_Do_list__c(Name='Work To Do  ' + ident, ObjectMap__c= pageid);
This was selected as the best answer