2

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.

| improve this question | | | | |
  • 2
    What controller code have you got so far? – Doug B Jul 2 '13 at 10:56
  • No one, I don't know how to do this ?? Recently Started SalesForce – Raman Jul 2 '13 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. – Peter Knolle Jul 2 '13 at 14:54
4

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
        Database.rollback(sp);
        ApexPages.addMessages(e);
    }
    return null;
  }
}
| improve this answer | | | | |

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.