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 vf page updating and creating

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 my handler code:

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>
ShivankurShivankur (Salesforce Developers) 
Hi Uday,

You might want to redirect on performing the database operation to desired location or window. Since, it must be following the default flow of showing all contacts upon save to the database.

Please try to follow below helper links to achieve the same:
https://developer.salesforce.com/forums/?id=906F0000000MJQpIAO
https://developer.salesforce.com/forums/?id=9060G0000005SOMQA2

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.