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
uday uday chavanuday uday chavan 

parent child relation on vf page

here i am displaying account and their related contacts also if user want to update the contact and account he can update and also he can create the contact related to account but problem is if i preview the page the related contacts are showing all contact before i put account id in link
it is working fine when i put id but it is showing all contacts before i put something 
also i am creating contact on same page so how i can give condition so if create account details are filled the it will create new contact(Note:lastname of contact is mandatory field)

here is handler:

public with sharing class AccExtension {
    Account acc;
    String queryString;
    Public Contact Con{get;set;}
    public list<Contact> listofContacts{get;set;}
        
    public AccExtension(ApexPages.StandardController controller){
        this.acc = (Account)controller.getRecord();
        Id accId = controller.getId();
        con = new Contact();
        listofContacts = new list<Contact>();
        queryString = 'select id';
        for(Schema.FieldSetMember fld :getfieldCon()) {
         queryString += ', ' + fld.getFieldPath();
        }
        queryString += ' from Contact where AccountId=:accId';
         
        listofContacts = Database.query(queryString);
        }
    
    
    public List<Schema.FieldSetMember> getfieldCon(){
        list<Schema.FieldSetMember> fsFieldsList = new list<Schema.FieldSetMember>();
        fsFieldsList = SObjectType.Contact.FieldSets.Contact_Info.getFields();
        return fsFieldsList;
    }
    
    
    
    public PageReference save(){
        update acc;
        update listofContacts;
        con.AccountId = acc.Id;
        insert con;
        return null;
    }
}

vf page

<apex:page standardController="Account" extensions="AccExtension" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection columns="2" title="Account information" >
                <apex:outputField value="{!Account.Name}" />
                <apex:outputField value="{!Account.Type}" />
                <apex:outputField value="{!Account.AccountNumber}" />
            </apex:pageBlockSection>   
             <apex:pageBlockSection columns="2" title="Contact information" >   
                <apex:repeat value="{!Account.contacts}" var="c"  >
                    <apex:repeat value="{!$ObjectType.Contact.FieldSets.Contact_Info}" var="f">
                        <apex:inputField value="{!c[f]}"/>
                    </apex:repeat>
                 </apex:repeat>s
                 </apex:pageBlockSection>
              <apex:pageBlockSection title="new contact" > 
                  <apex:repeat value="{!Account.contacts}" var="c"  >
                    <apex:repeat value="{!$ObjectType.Contact.FieldSets.Contact_Info}" var="f">
                        <apex:inputField value="{!c[f]}"/>
                    </apex:repeat>
                 </apex:repeat>
                  <apex:inputField value="{!cont.LastName}" required="false" /><br/>
            <apex:commandButton value="Save" action="{!Save}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>