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
NikunjVadiNikunjVadi 

Passing ID between two vf pages

I Am creating Child record(Home_Model__c) of account object  Using this controller:

public class HomeModelController {
    Apexpages.StandardController controller;
    public Home_Model__c myCustomObject;
    public HomeModelController(ApexPages.StandardController sc)
    { 
    this.controller = sc;
    myCustomObject = (Home_Model__c) sc.getRecord();  
    }

// Create New record and Redirect  //Start Button Functionality  
    public PageReference New_HomeModel()
    {
          PageReference ret = controller.save();        
          Id pageid = controller.getId();
          Pagereference ref = new Pagereference('/apex/HM_Entrance);
          ref.setRedirect(true);
          return ref;
    }
}

Now Have another child object of Home Model Page. Entrance__C. Now I am having error becuase controller are diffrent :
"System.TypeException: Invalid conversion from runtime type SOBJECT:Entrance__c to SOBJECT:Home_Model__c "  
I want to use the created Home_model__c record id as a parent id of child record of Entrance .

I am not able to know how to approch this. How can i pass the id from one page to another ? 


 
Best Answer chosen by NikunjVadi
SarfarajSarfaraj
Hi Nikunj

I am assuming you are trying to redirect the user to new child record page. I do not have the custom objects in my org. Let me show you an example with account and contact object,

Page: Home
<apex:page standardController="Account" extensions="HomeController">

<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!New_HomeModel}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:inputField value="{!Account.Name}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

</apex:page>
Controller extension:
public with sharing class HomeController {

    Apexpages.StandardController controller;
    public Account myCustomObject;
    public HomeController(ApexPages.StandardController sc)
    { 
    this.controller = sc;
    myCustomObject = (Account) sc.getRecord();  
    }

    public PageReference New_HomeModel()
    {
          upsert myCustomObject;
          Pagereference ref = new Pagereference('/apex/HM_Entrance');//Page.HM_Entrance;
          ref.getParameters().put('accountId', myCustomObject.Id);
          ref.setRedirect(true);
          return ref;
    }

}

Page: HM_Entrance:
 
<apex:page standardController="Contact" extensions="EntranceExt">
    
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!Contact.AccountId}"/>
                <apex:inputField value="{!Contact.FirstName}"/>
                <apex:inputField value="{!Contact.LastName}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>

</apex:page>
Controller Extension:
public with sharing class EntranceExt {

    public EntranceExt(ApexPages.StandardController controller) {
        Contact c = (Contact) controller.getRecord();
        c.AccountId = ApexPages.currentPage().getParameters().get('accountId');
    }

}

Let me know if this helps.

 

All Answers

ericmonteericmonte
I'm not sure what you are trying to do here. Are you trying to redirect the user to the New child record of the account after you create it?
SarfarajSarfaraj
Hi Nikunj

I am assuming you are trying to redirect the user to new child record page. I do not have the custom objects in my org. Let me show you an example with account and contact object,

Page: Home
<apex:page standardController="Account" extensions="HomeController">

<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!New_HomeModel}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:inputField value="{!Account.Name}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

</apex:page>
Controller extension:
public with sharing class HomeController {

    Apexpages.StandardController controller;
    public Account myCustomObject;
    public HomeController(ApexPages.StandardController sc)
    { 
    this.controller = sc;
    myCustomObject = (Account) sc.getRecord();  
    }

    public PageReference New_HomeModel()
    {
          upsert myCustomObject;
          Pagereference ref = new Pagereference('/apex/HM_Entrance');//Page.HM_Entrance;
          ref.getParameters().put('accountId', myCustomObject.Id);
          ref.setRedirect(true);
          return ref;
    }

}

Page: HM_Entrance:
 
<apex:page standardController="Contact" extensions="EntranceExt">
    
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!Contact.AccountId}"/>
                <apex:inputField value="{!Contact.FirstName}"/>
                <apex:inputField value="{!Contact.LastName}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>

</apex:page>
Controller Extension:
public with sharing class EntranceExt {

    public EntranceExt(ApexPages.StandardController controller) {
        Contact c = (Contact) controller.getRecord();
        c.AccountId = ApexPages.currentPage().getParameters().get('accountId');
    }

}

Let me know if this helps.

 
This was selected as the best answer
NikunjVadiNikunjVadi
Thanks for the response,


Here is the senario:
Account parent record,
Home_model__C child record of account,
Entrance child record of Home_model__c,


On First page i am creating record and passing  that new record value to next page . On the second page i am creating child record(Entrance__c) of the parent (Home_Model__c) Record. Now controller of both page are diffrent , so i am getting error. when i goto second page. so i am not getting id of the new created Parent Record(Home_Model__c) that i am using in second page as parent id of child record i am creating (Entrace__c).
Is it clearn now
ericmonteericmonte
You can do it in a few ways.
1. If your second page is a child record of the first page then you can pull the parent recordId using a SOQL.
2. You can pass the parentId when you redirect to another page and use the get parameters to save the parentId into a variable
3. You can also use transient variables if you want to send it across.
NikunjVadiNikunjVadi
Hey,
Thanks for the reply
I am approching another method to create child record :
Here is the my code

HM_Homeaccount Page:
<apex:page standardController="Home_Model__c" extensions="HomeModelController">
    <apex:form >
        <apex:pageBlock >
            <h1>Please Select the Home Account of the Client of Which You would like to take House's Information.</h1>
            <br/><br/><br/>
            <apex:pageblockSection >
                <apex:pageBlockSectionItem >
                    <strong>Account</strong> &nbsp;&nbsp;&nbsp;
                    <apex:inputField value="{!Home_Model__c.Home_Model_Account_Mapping__c}"/>
                </apex:pageBlockSectionItem>
            </apex:pageblockSection>
            <apex:pageBlockButtons ><apex:commandButton action="{!New_HomeModel}" value="Start" id="Start"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

HomeModelController: Removing unnecessary code here
public class HomeModelController {
    Apexpages.StandardController controller;
    public Home_Model__c myCustomObject;
    public Boolean Ventilationgrilles;
        
    public HomeModelController(ApexPages.StandardController sc)
    { 
    this.controller = sc;
    myCustomObject = (Home_Model__c) sc.getRecord();  
    }

// Create New record and Redirect  //Start Button Functionality  
    public PageReference New_HomeModel()
    {
          PageReference ret = controller.save();        
          Id pageid = controller.getId();
          Pagereference ref = new Pagereference('/apex/HM_Entrance?id='+pageid);
          ref.setRedirect(true);
          return ref;
    }

// Create New record and Redirect  //Start Button Functionality  
    public PageReference AddEntrance()
    {
                
          Id pageid = ApexPages.currentPage().getParameters().get('id');
          Pagereference ref = new Pagereference('/apex/HM_Entrance?id'+pageid);
          ref.setRedirect(true);
          return ref;
    }
    
    public class AddEntrance
    {
    public Entrance__c ent {get; private set;}  
   // ent=new Entrance__c(HomeModel_Entrance_Mapping__c=pageid);
  //  insert ent;
    }
   
}
HM_Entrance Page:
<apex:page standardController="Home_Model__c" extensions="HomeModelController">
    <apex:form >
        <apex:pageBlock >
            <h1>Entrance</h1>
            <br/><br/><br/>
            <apex:pageblockSection columns="1">
            </apex:pageblockSection>
            
            <strong>Level</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:SelectList size="1" Title="Level">
            <apex:selectOptions value="{!Level}"></apex:selectOptions>
            </apex:SelectList>
            
            <br/><br/><strong>No. of Window Panels</strong>&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:SelectList size="1" Title="Number of Window Panels">
            <apex:selectOptions value="{!WindowPanels}"></apex:selectOptions>
            </apex:SelectList>
            
            
            <apex:pageBlockButtons ><apex:commandButton action="{!Save}" value="Next" id="theButton"/> &nbsp; &nbsp;
            <apex:commandButton action="{!Save}" value="Add Another Entrance" id="AddEntrance"/>
            </apex:pageBlockButtons>
            
            </apex:pageBlock>
    </apex:form>
</apex:page>


Before i was using diffrent standard controller : so i was having issue, now i am passing the value by taking the value by get set method and creating child record.

In Addentrance pagereference i would like to create child record using current pageid as parent id and  value of 
Ventilationgrilles in two custom field of entrance__c custom object. 
Can you help me out here ?

Thanks
ericmonteericmonte
public PageReference AddEntrance()
    {
                
          Id pageid = ApexPages.currentPage().getParameters().get('id');

Entrance__c ent = new Entrance__c
(
parentField__c = pageid,
customfield__c = 'insert field'
);

insert ent;

          Pagereference ref = new Pagereference('/' +ent.id);
          ref.setRedirect(true);
          return ref;
    }
Try this out. Hopefully this is what you are getting at.
 
NikunjVadiNikunjVadi
Thanks Man