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
HunnyHunny 

How to show account details in another form of Visualforce Page

In Visualforce Page i created a two form. In first form i called field set to create contact and in second form i have to show the related account details of contact. And we are able to update both contact and account in same page. but i am not able to show related account details in 2nd form.

here is my code:
<apex:page Controller="DemoController1"  showHeader="false">
    <html>
        <h1><center><font size="8"><b>Satrang Technologies</b></font></center></h1>
    
   <!-- <apex:form id="form1" rendered="{!form1}">        
        <apex:pageBlock title="Account Details">
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Name}"/>
                <apex:inputField value="{!acc.Phone}"/>
                <apex:inputField value="{!acc.AccountNumber}"/>
                
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="save" onclick="myFunction()" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>-->
    <apex:form id="form1" rendered="{!form1}">
        <apex:pageBlock title="Contact Details">
            <apex:pageBlockSection >
                <apex:repeat value="{!fields}" var="f">
                  <apex:inputField value="{!con[f.fieldPath]}" 
                      required="{!OR(f.required, f.dbrequired)}"/>
              </apex:repeat>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="save" onclick="myFunction()" action="{!save}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
	<apex:form id="form2" rendered="{!form2}">        
        <apex:pageBlock title="Account Details">
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Name}"/>
                <apex:inputField value="{!acc.Phone}"/>
                <apex:inputField value="{!acc.AccountNumber}"/>
                
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="save" action="{!save1}" rerender="form2"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>        
                   
    </html>
</apex:page>

Controller class:
public with sharing class DemoController1{
    public Account acc {set;get;}
    public Contact con {set;get;}
    public  Boolean form1 {set;get;}
    public Boolean form2 {set;get;}
    public DemoController1(){
        form1=true;
        form2=false;
        con = new Contact();
    }
    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.Contact.FieldSets.SatarangSet.getFields();
    }
    
    public pagereference save(){
        //form1=false;
        form2=true;
        upsert con;         
        return null;
        
    }
    public pagereference save1()
    {
      System.debug(acc.id);
        update acc;         
        return null;
        
    }
    
    
}

 
Maharajan CMaharajan C
Hi Hunny,

Please confirm this:

1. in the contact form do we have Account Lookup Field to fill?(Did you added the Account Lookup in FieldSet from Contact)
2. If the 1st point is yes then you want to fetch above lookuped value in the form 2:
3. If 1st and 2nd point is correct then use the below code :


public with sharing class DemoController1{
    public Account acc {set;get;}
    public Contact con {set;get;}
    public  Boolean form1 {set;get;}
    public Boolean form2 {set;get;}
    public DemoController1(){
        form1=true;
        form2=false;
        con = new Contact();
    }
    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.Contact.FieldSets.SatarangSet.getFields();
    }
    
    public pagereference save(){
        //form1=false;
        form2=true;
        upsert con; 
        acc= [Select Id,Name,Phone,AccountNumber from Account where Id =: con.AccountId];  
        return null;
        
    }
    public pagereference save1()
    {
      System.debug(acc.id);
        update acc;    
        
       // PageReference nextPage = new PageReference('/' + acc.Id);
        //nextPage.setRedirect(true);
       // return nextPage;

        return null;
        
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C