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
SFDCDevQASFDCDevQA 

Creating two objects at once

I have a custom object for Demos with a multi relationship to Contacts meaning that a contact could have multiple demos and a demo has multiple contacts.  For this multi relationship to work there is a custom Demo Contact object that simply has two fields - Contact and Demo.  Currently the users have to create the Demo Object and then manually tie it back to a contact object.  I'd like to make it so that they can easily press a button and behind the scenes it will Create a blank new Demo record, tie it to a new demo contact record for that contact and then direct them to the edit page for the Demo.  So to them they would just click on "New Demo" and fill in the information and the Contact would automatically be there in the related list when they hit save.  I've tried to do this with custom buttons but I have issues that they won't auto save for me.

 

Any ideas would be highly appreciated.

 

Thanks,

Amanda

Best Answer chosen by Admin (Salesforce Developers) 
PeterMosherPeterMosher

I would create a Visualforce page that calls an apex page.

This visualforce page would basically be empty and would redirect to where ever you wanted to go afterwords.

 

The apex page would create your objects and insert them into the database.

There are a couple of gotchas.

1) any fields that you want to transfer data from have to be part of the visualforce page

2) you cannot do DML operations in a constructor.

 

as far as apex:
Demo__c tutorial1 =  new Demo__c (

  Name = 'some name'

);

 

insert(tutorial1);

 

Contact the_contact = new Contact (

  Name = 'John',

  //other fields are necessary

);

 

insert(the_contact);

 

tutorial1.Contact__c = the_contact.Id;

 

update(tutorial1);

 

 

The button could just be a URL:

https://[the domain]/[Id of your record]

you can do this dynamically too.

 

 

You will need to do a PageReference and create a Visualforce controller. All your logic would be in this Visualforce Controller.

All Answers

PeterMosherPeterMosher

I would create a Visualforce page that calls an apex page.

This visualforce page would basically be empty and would redirect to where ever you wanted to go afterwords.

 

The apex page would create your objects and insert them into the database.

There are a couple of gotchas.

1) any fields that you want to transfer data from have to be part of the visualforce page

2) you cannot do DML operations in a constructor.

 

as far as apex:
Demo__c tutorial1 =  new Demo__c (

  Name = 'some name'

);

 

insert(tutorial1);

 

Contact the_contact = new Contact (

  Name = 'John',

  //other fields are necessary

);

 

insert(the_contact);

 

tutorial1.Contact__c = the_contact.Id;

 

update(tutorial1);

 

 

The button could just be a URL:

https://[the domain]/[Id of your record]

you can do this dynamically too.

 

 

You will need to do a PageReference and create a Visualforce controller. All your logic would be in this Visualforce Controller.

This was selected as the best answer
SFDCDevQASFDCDevQA

I was afraid you would say that but in the end it wasn't as hard as I was fearing.  Here's the finished class and the VF page was practically nothing:

 

public class NewDemofromContactExtension {
    
     //added an instance variable for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private Contact con {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
 
    // initialize the controller
    public NewDemofromContactExtension(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        con = (Contact)controller.getRecord();
       } 
         // method called from the VF's action attribute to create the New Demo
    public PageReference NewDemo() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Demo__c newDemo;
         Demo_Contact__c newDemoCON;
 
         try {
 
              //get information from opp to go to Demo - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             con = [select Id, Name, OwnerID, Account.Academic_Calendar__c from Contact where id = :con.id];
             newDemo = New Demo__c();
             newDemo.AM_RM__c = con.OwnerID;
             newDemo.Academic_Calendar__c = con.Account.Academic_Calendar__c;
             insert newDemo;
             
             // set the id of the new Demo created for testing
               newRecordId = newDemo.id;
             
    // create the Opportunity_demo connecting record
            newDemoCON = New Demo_Contact__c();
                  newDemoCON.Demo__c = newDemo.id;
                  newDemoCON.Contact__c = con.id;
                  insert newDemoCON;
             
               } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }
  
        return new PageReference('/'+newDemo.id+'/e?retURL=%2F'+newDemo.id);
        
    }

}