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
Mohammadasif SiddiquiMohammadasif Siddiqui 

Create a VF page to display child record base on the parent record and Include edit link on each child record.

I have to complete this task, can anyone provide me code

Thanks in advance

Best Answer chosen by Mohammadasif Siddiqui
Mohammadasif SiddiquiMohammadasif Siddiqui
<apex:page controller="Task2Apex">
    <apex:form >
        <apex:pageBlock title="Account Name" >
            <apex:selectList value="{!selectedAccId}" size="1">
                <apex:selectOptions value="{!AccountNames}" />
                <apex:actionSupport event="onchange" action="{!showContacts}" reRender="op" />
            </apex:selectList><br/><br/>
            
         <apex:outputPanel id="op">
            <b><apex:outputText value="Related Contacts" rendered="{!renderPBtable}"/></b>
            <b><apex:outputText value="No Contacts on this Account" rendered="{!noContact}" style="color: red;"/></b>
            <apex:pageblockTable title="Contacts" value="{!conlist}" var="sc" id="relatedContactsBlock" rendered="{!renderPBtable}">
                <apex:column width="100" headerValue="Actions">
                     <apex:outputLink value="/{!sc.id}/e">Edit</apex:outputLink>
                </apex:column>
                <apex:column value="{!sc.name}"/>
                <apex:column value="{!sc.phone}"/>
            </apex:pageblockTable>
        </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class Task2Apex {

    public Id selectedAccId{get;set;} 

    public List<Contact> conlist {get;set;}   
    
    public Boolean renderPBtable {get;set;}
    
    public Boolean noContact {get;set;}

    public List<SelectOption> getAccountNames() {
        List<SelectOption> accOptions= new List<SelectOption>();
        system.debug(selectedAccId);
        accOptions.add( new SelectOption('','--Select--'));
        for(Account acc : [select Id,name from Account ] ) {
            accOptions.add(new SelectOption(acc.Id,acc.name));
        }
        return accOptions;
    }

    public PageReference showContacts(){
        conlist=[select name,phone from Contact where Accountid=: selectedAccId];
        if(conlist.size() > 0)
        {
            renderPBtable = true; 
            noContact = false;
        }
        else
        {
            renderPBtable = false;
            noContact = true;
        }
        return null;   
    }  
}