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
Phuc Nguyen 18Phuc Nguyen 18 

Visualforce Page show parent record on Child creation

Hello All,
I have a VF page where I need to show the parent lookup field on the vf page. 
This page is the creation page of the child record.  
I have an extension but not able to grab the parent Id.
Extension is on the child object
Parent is AutoM__c
Child is Rental__c
Able to get fileds for child record but not parent.

Thanks,
P
Best Answer chosen by Phuc Nguyen 18
Abhishek BansalAbhishek Bansal
Hi,

You can query parent fields like this:
Class Code:
public Rental__c childRecord {get;set;}

public yourClass(){
    childRecord = [SELECT Id, Name, AutoM__r.Name FROM Rental__c WHERE id = :ApexPages.currentPage().getParameters().get('id')];
}


VF Page:
{!childRecord.AutoM__r.Name}
Let me know if you need any more information on this.

Thanks,
Abhishek Bansal.

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Phuc,

Try the Code Snippet as mentioned in the below discussion,

https://salesforce.stackexchange.com/questions/197267/how-to-create-the-child-record-from-parent-object-vf-page

For further reference, you can check this,

http://forceforte.com/how-to-insert-parent-record-and-list-of-child-records-in-visualforce/

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Phuc Nguyen 18Phuc Nguyen 18
thanks for the reply Abhishek.  Getting closer.  Now I just need the parent name to render instead of the id value.  Tried AutoM__r.Name and it did not render anythingwhile AutoM__c renders the id.
Abhishek BansalAbhishek Bansal
Hi ,

You need to query the Name field from the Parent object. By default only Id is available so if you want to reference any other fields than please make sure those fields are queried from the database.
Let me know if you need any further information on this.

Thanks,
Abhishek Bansal.
Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply Abhishek but how do I make sure I am getting the parent:
ex parentid = [SELECT Id, Name.... FROM AutoM__c WHERE id = :????;

How do I reference the field in the visualforce page?
Thanks
Abhishek BansalAbhishek Bansal
Hi,

You can query parent fields like this:
Class Code:
public Rental__c childRecord {get;set;}

public yourClass(){
    childRecord = [SELECT Id, Name, AutoM__r.Name FROM Rental__c WHERE id = :ApexPages.currentPage().getParameters().get('id')];
}


VF Page:
{!childRecord.AutoM__r.Name}
Let me know if you need any more information on this.

Thanks,
Abhishek Bansal.
This was selected as the best answer