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
Dutta SouravDutta Sourav 

Displaying Parent Object on Child Record through VF Page

I want to display the parent Object on Child Record where there is Master-Details relationship.

There are two Objects: Student(Child) & College (Parent).

I want to design a form in Visualforce to insert Student Details where I want to take College as input.

But, Its showing error.

Can anyone help?

Best Answer chosen by Dutta Sourav
SipuSipu
Sourav Master lookup behavior is you can add new record but you cannot edit that record after add. That is the reason for hide the lookup icon.

See following description for master lookup field

Creates a special type of parent-child relationship between this object (the child, or "detail") and another object (the parent, or "master") where:
  • The relationship field is required on all detail records.
  • Once the value of the relationship field has been saved, it cannot be changed.
  • The ownership and sharing of a detail record are determined by the master record.
  • When a user deletes the master record, all detail records are deleted.
  • You can create rollup summary fields on the master record to summarize the detail records.

The relationship field allows users to click on a lookup icon to select a value from a popup list. The master object is the source of the values in the list.

All Answers

SipuSipu
Hi Sourav,
In your visualforce page simply use <apex:inputField/> attribute and give the proper value i.e the master detail field name. 
Dutta SouravDutta Sourav

Hi Sameer,

I have done that only. In that case only the field name is displaying.

It's not allowing to accept values.

SipuSipu
Hi Sourav
what is the error you are getting ? Can you please share the code where you are getting errors. 
Dutta SouravDutta Sourav

Sameer,

Sharing the code:

 

VF Page:

<apex:page controller="recordstd">
    <apex:form >
        <apex:pageBlock title="New Record">
            <apex:pageBlockButtons >
                <apex:commandButton value="save" action="{!ssave}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Information">
                <apex:inputField value="{!sc.sname__c}" required="true"/>
                <apex:inputField value="{!sc.College_Name__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public with sharing class recordstd {
public PageReference ssave() {
    insert sc;

    PageReference  ref= new PageReference('/'+sc.id);
        return ref;
    }
public Student__c sc{get; set;}
}

The Displayed Error is:
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!ssave}' in component <apex:commandButton> in page controller2: Class.recordstd.ssave: line 5, column 1

 

SipuSipu
Sourav,,
Just add the constructor. 

Add the following code . 

public void recordstd(){
   sc = new student__c () ;  
}


happy coding ..
Dutta SouravDutta Sourav

Still the error is showing. Problem is that the college field is not showing on the VF Page.

This is the Snapshot of the VF Page

Further help is appreciated.

SipuSipu
Sourav Master lookup behavior is you can add new record but you cannot edit that record after add. That is the reason for hide the lookup icon.

See following description for master lookup field

Creates a special type of parent-child relationship between this object (the child, or "detail") and another object (the parent, or "master") where:
  • The relationship field is required on all detail records.
  • Once the value of the relationship field has been saved, it cannot be changed.
  • The ownership and sharing of a detail record are determined by the master record.
  • When a user deletes the master record, all detail records are deleted.
  • You can create rollup summary fields on the master record to summarize the detail records.

The relationship field allows users to click on a lookup icon to select a value from a popup list. The master object is the source of the values in the list.
This was selected as the best answer