I have custom objects A,B,C,D which has look up relation in another object E. Now I want to create one VF page in which all all the fields of each object will come and on click of save all record will be saved. Means on one VF page I wnt to save multiple objects record. I know that I need to create the controller. Please explain in detail with code.

  • 2
    What controller code have you got so far?
    – Doug B
    Commented Jul 2, 2013 at 10:56
  • No one, I don't know how to do this ?? Recently Started SalesForce
    – Raman
    Commented Jul 2, 2013 at 12:38
  • 2
    @Raman - You can take a look at the documentation for more information about querying records (SOQL), saving records (DML), Apex, and Visualforce. The only thing that is slightly out of the ordinary with saving multiple records is the transaction control part that I sketched out in my answer. It is not likely that someone here will write all of the code for you. Commented Jul 2, 2013 at 14:54

1 Answer 1


You most likely want to implement transaction control to ensure that all saves succeed or fail together. Here's a rough outline.

public class MyController {

  public ObjA recA { get; set; }
  public ObjB recB { get; set; }
  public Objc recc { get; set; }
  public ObjD recD { get; set; }
  public ObjE recE { get; set; }

  public MyController {
      // query and set recs

  public PageReference saveAll() {
    // set a save point so that if one rec save
    // fails they all fail.
    Savepoint sp = Database.setSavepoint();

    try {
        upsert recE;

        // perhaps set the lookup field
        recD.recE__c = recE.Id;

        // save others
        // upsert, etc...
    } catch (Exception e) {
        // roll back if there is an error in saving
        // any of the records
    return null;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .