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
Aditya Rayavarapu 5Aditya Rayavarapu 5 

Insert Parent and Child in one step

Hello,
I have a lookup relationship between Contact (Master) and Dependant__c (Child). The lookup field is called Primary__c.
I have a VF page where you can enter the contact details, and also the dependant details. When you click 'Save', both the contact and dependant save. However, I want the dependant to have a lookup relation to this specific contact.  How can I achieve this? I have tried to get the record ID of the contact before inserting the dependant, but I cannot get the getting the Id part of the code right.

VF PAGE (simplified) :

<apex:page controller="customcontroller">
<apex:form>
           <apex:inputfield value="{!c.LastName}" />
           <apex:inputfield value="{!d.Name__c}"/>

        <apex:commandButton action="{!save}" value="Save"/>
</apex:form>
</apex:page>

CONTROLLER:

public class CustomController {

public Contact c {get; set;}
public Dependant__c d {get;set;}

public void save(){
    
        insert c;
Primary__c=('Select Name from Contact') //unclear about getting the Contact record ID for this part
insert d;
}
}
       
       
        Thanks,
Adi
Best Answer chosen by Aditya Rayavarapu 5
bob_buzzardbob_buzzard
You can do this, but it requires jumping through a few hoops :

http://bobbuzzard.blogspot.co.uk/2012/03/create-parent-and-child-records-in-one.html

All Answers

bob_buzzardbob_buzzard
You can do this, but it requires jumping through a few hoops :

http://bobbuzzard.blogspot.co.uk/2012/03/create-parent-and-child-records-in-one.html
This was selected as the best answer
Aditya Rayavarapu 5Aditya Rayavarapu 5
Thank you Bob, that helps a ton!
Big fan of your blog!