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
apsullivanapsullivan 

Visualforce Page to Create Case with A Dynamic Number of Child Records

I would like to create a Visualforce page which allows a user to create a Case and multiple child records (called Case Campaigns) at once.

The problem I am trying to solve is that we want to use Cases to setup new campaigns, and have it be only one case per setup, but be able to define multiple budgets for each device.  So for example, one case for a setup, but 4 devices with 4 different budgets.  The best way I can think to achieve this is by using child records on the cases to represent the specific details on a granular level, but creating them one-by-one is time-consuming and tedious.  I want the user to be able to create them all on one page along with the new case.

Ideally, the flow works as follows:
  1. User clicks on a button to create a new case which redirects to this VF page
  2. The Case has 4-5 "core" fields that are just Case fields as I've defined them
  3. The user selects a number from a Picklist Field called "Number of Campaigns"
    1. Eg. The user selects the number "5" in the Picklist
  4. The page then reloads a block that shows the number of Case Campaigns listed as line items
    1. Eg. There are now 5 lines, each with a device and a budget
  5. The user performs a save, which inserts the new Case and the new Case Campaigns at once
Is this possible?  It seems a bit wacky to create a parent and child at once, so I am worried that it is not possible.  An alternative would be to just have the user create the case, then have a "New Case Campaigns" button which again redirects to a VF page, and which allows the user to again choose a dynamic number of Case Campaigns to create, then insert multiple at once.

I would love a code example if anyone has written a blog post anywhere or has faced such a challenge in the past.  Thanks very much in advance!
Ashish_SFDCAshish_SFDC
Hi, 


Try the Clone function which can be called to create and insert new records, 

See the below links for code and more information, 

Insert,Edit,Delete,Clone Records for an object from Vfpage

http://flexandsalesforce.blogspot.com/2011/07/inserteditdeleteclone-records-for.html

Apex “Deep Clone” Controller

http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/


Regards,
Ashish