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
Shivani BankarShivani Bankar 

passing master data to detail object on vf page

hello,
I want to create wizard such a that, I'm having two object with master detail relationship
on first VF page master object's data is inserted and when we Click on save button Detail object VF page should open with Master Object's name already inserted on it 
Pradeep SinghPradeep Singh
Hi, Refer Below code.
---------------PAGE 1------------------------Name :- AccConFlow1
<apex:page controller="AccConOppFlow1">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Next" action="{!saveacc}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:inputField value="{!acc.name}"/>
            <apex:inputField value="{!acc.Rating}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>    
</apex:page>

--------------------------------------------------------------
---------------------------PAGE 2---------------------------------------Name : AccConFlow2
<apex:page controller="AccConOppFlow1">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!saveCon}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:inputField value="{!Con.Firstname}"/>
            <apex:inputField value="{!Con.LastName}"/>
            <apex:inputField value="{!con.AccountId}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>    
</apex:page>

--------------------------------------------------------------
Public class AccConOppFlow1{
    Public account acc{get;set;}
    Public Contact con{get;set;}
    
    Public AccConOppFlow1(){
        acc= new account();
        con = new contact();
        string accid = apexpages.currentpage().getparameters().get('accId');
        if(accid != Null){
            con.AccountId = accid;
        }
    }
    
    Public Pagereference saveacc(){
        insert acc;
        Pagereference p = new pageReference('/apex/AccConFlow2?accId='+acc.id);
        return p;
    }

    Public pageReference saveCon(){
        insert con;
        pagereference pg = new pageReference('/'+acc.id);
        return pg;
    }

}

If this solves your problem, please mark it as best answer/solved.
<Saket><Saket>
Hi Shivani

You can do this by the following approach below

1. In your first vf page in save call controller method u must be inserting master data after inserting u will know about the id of that inserted record so that when u redirect to the next detail VF page append this is in query parameter like for ex- https://saketorg-dev-ed--econtacts.ap5.visual.force.com/apex/DetailVFPage?Id=YOUR MASTER RECORD ID.

2. In detail VF page in constructor u can get the Id of the the master by this code snippet 
       Id masterId = ApexPages.currentPage().getParameters().get('Id');

3. After retrieving Id query the Master to retrieve the information u want to display in vf.

I think this will solve ur problem, if u still have any problem please let me know.

Thanks 
Saket Sharma