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
bperry8086bperry8086 

Saving two objects from one VF page?

I'm trying to streamline data entry for my organization.  We've got two objects that could be improved by a visualforce page that's cut down to just the fields used by the data entry team.  It would be even better if we could get the Save button on that page to save the two objects that get created from that.  The objects in question are a Contact and a custom object that points to the new contact.

 

I'm currently using a controller extension to save a contact and redirect the user back to the data entry page when the Save button gets pressed.  I'm thinking I need to do something like getting the ID of the contact I've just saved and then maybe call a custom controller for the custom object.  Is it possible to call a custom controller for another object from a controller extension?

 

Can anyone point me to examples of how to do something like this?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bperry8086bperry8086

Thanks for setting me on the right track.

 

There's some details missing from the original response, so here's stripped-down version of my current code.  It also sets the record type of the contact and redirects the user back to an edit page when done.

 

public class FastContactEntryPageController {

private ApexPages.StandardController controller;

public FastContactEntryPageController(ApexPages.StandardController controller) 
{
this.controller = controller;
}

public PageReference saveAndBackToEdit() {
String ContactID;
Contact ct;

RecordType rt = new RecordType();
ct = (Contact)controller.getRecord();
ct.recordtypeid = '012000000000gKl';

insert ct;
//controller.save(); // This takes care of the details for you.  
  
ContactID = ct.ID;

Scholar_Program_Application__c sp = new Scholar_Program_Application__c();

//make the new scholar program point to the contact we just saved
sp.Scholar_Name__c = ContactID;  

/*
... set variables on new scholar application ...
*/

insert sp;

  PageReference FastContactEditPage = Page.FastContactEntry;
  FastContactEditPage.setRedirect(true);
  return FastContactEditPage;
}

 

All Answers

pjcocopjcoco

in your Extension Method, override the save() function, basically just create your own save function, in this function save your Contact. After you inserted your contact, the original reference gets populated with the id.

 

 

 

public PageReference save(){
	insert contact;
	Custom_Object__c o = new Custom_Object__c();
	o.Contact__c = contact.Id;
	insert o;
	return new PageReference();
}

 

bperry8086bperry8086

Thanks for setting me on the right track.

 

There's some details missing from the original response, so here's stripped-down version of my current code.  It also sets the record type of the contact and redirects the user back to an edit page when done.

 

public class FastContactEntryPageController {

private ApexPages.StandardController controller;

public FastContactEntryPageController(ApexPages.StandardController controller) 
{
this.controller = controller;
}

public PageReference saveAndBackToEdit() {
String ContactID;
Contact ct;

RecordType rt = new RecordType();
ct = (Contact)controller.getRecord();
ct.recordtypeid = '012000000000gKl';

insert ct;
//controller.save(); // This takes care of the details for you.  
  
ContactID = ct.ID;

Scholar_Program_Application__c sp = new Scholar_Program_Application__c();

//make the new scholar program point to the contact we just saved
sp.Scholar_Name__c = ContactID;  

/*
... set variables on new scholar application ...
*/

insert sp;

  PageReference FastContactEditPage = Page.FastContactEntry;
  FastContactEditPage.setRedirect(true);
  return FastContactEditPage;
}

 

This was selected as the best answer