+ Start a Discussion
Maddy6161Maddy6161 

Hi! I want to create a VF form where i retrieve all account name in drop down field, when i select any account name then the relate field will be shown?? is it possible ?? or any code for that

Account : Maddy  - drop-down filed when i selected Maddy i want to retrive Fax, Phone, Website fields will be showned automatically

Fax:  ______________  these field automatically come when i select account name from dropdown
Phone:_____________
Wesite:_____________

 
Srinivas SSrinivas S
Hi Maddy,

Please find the following solution -

Controller Class:
public with sharing class AccDropdownRelFieldsContrl {
    public String selVal {get;set;}
    Map<String,Account> accNameAccsMap = new Map<String,Account>();
    public List<SelectOption> accountNames {get;set;}
    public AccDropdownRelFieldsContrl() {
        selVal = '--None--';   
        for(Account acc : [select Name, Fax, Phone, Website from Account limit: Limits.getLimitQueryRows()]) {
            accNameAccsMap.put(acc.Name,acc);
        }
        accountNames = new List<SelectOption>();
        accountNames.add(new SelectOption('--None--','--None--'));
        for(String accName : accNameAccsMap.keyset()) {
            accountNames.add(new SelectOption(accName,accName));
        }
    }
    
    public Account getAccount() {
        if(accNameAccsMap.containsKey(selVal))
            return accNameAccsMap.get(selVal);
        
        else
            return new Account();
        
    }
}

Visualforce Page: (Give some Name like 'AccountInfo')
<apex:page controller="AccDropdownRelFieldsContrl" tabStyle="Account">
    <apex:form >
        <apex:pageBlock mode="Edit" Title="Account">
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account"/>
                    <apex:selectList size="1" value="{!selVal}">
                        <apex:selectOptions value="{!accountNames}"/>
                        <apex:actionSupport event="onchange" reRender="accInfoSec"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:outputPanel id="accInfoSec">            
                <apex:pageblockSection title="{!account.Name}" rendered="{!selVal != '--None--'}">
                    <apex:outputField value="{!account.Fax}"/>
                    <apex:outputField value="{!account.Phone}"/>
                    <apex:outputField value="{!account.Website}"/>
                </apex:pageblockSection>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
Anil kumar GorantalaAnil kumar Gorantala
Hi maddy: you can try this one too:
controller:
public class accountcontroller {

    public string accid {get; set;}
    public Account SelectedAccount{get;set;}
    
    public list<selectoption> getaccountnames() {
        
        list<selectoption> accoptions = new list<selectoption>();
        for (account acc : [select id, name from account]){
            accoptions.add(new selectoption(acc.id, acc.name));
        }  
        return accoptions;
    } 
    
    public void  getDetails() {
        
        SelectedAccount = new Account();
        if(accid!='None')
        SelectedAccount = [SELECT id, Name, Phone,fax,website FROM Account WHERE id=:accid ];
        
    }
}

Vf page:
<apex:page Controller="accountcontroller" >
    <apex:form >
        <apex:pageBlock title="Display Account Details: ">
            <apex:pageBlockSection columns="1">
            <apex:outputLabel value="Account Name: " />
            <apex:selectlist value="{!accid}" size="1" >
                <apex:selectOption itemLabel="None" itemValue="None"></apex:selectOption>
                <apex:selectOptions value="{!accountnames}" />
                <apex:actionSupport action="{!getDetails}" event="onchange" rerender="display"/>
            </apex:selectlist>
            </apex:pageBlockSection>
        <apex:pageBlockSection id="display" title="Account Details">
            <apex:outputField value="{!SelectedAccount.Phone}"/>
            <apex:outputField value="{!SelectedAccount.fax}"/>
            <apex:outputField value="{!SelectedAccount.Website}"/>
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please mark as best answer if it solved your problem.
Maddy6161Maddy6161
Thanks Srinivas, Can u tell me one more thing can i edit Phone, fax, website field
 
Anil kumar GorantalaAnil kumar Gorantala
add inline edit support tag to the field please refer this: https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/visualforce_6.htm
mritzimritzi
Try this piece of code

Apex:
public class TestClass2 {
  // Map of accounts fetched from database
  Map<Id,Account> accountMap;
  // list used to populate pickilst on VF page
  List<SelectOption> accountPL;
  //used to hold detail of selected account
  public Account ac{get;set;}
  
  public TestClass2(){
      accountPL=new List<SelectOPtion>();
      ac=new Account();
      try{
          accountMap = new Map<Id,Account>([Select id,Name,Fax,Phone,Website From Account Order by Name]);
          
          accountPL.add(new SelectOption('','--None--'));
          Set<Id> acIds = accountMap.keySet();
          for(Id i:acIds){
              Account ac  = accountMap.get(i);
              accountPL.add(new SelectOption(ac.Id,ac.Name));
          }
      }catch(Exception e){
          System.debug(e);
      }
  }
  // used to display Account details based on picklist value
  public void showValues(){
      if(ac.id==null)
          ac=new Account();
      else
          ac = accountMap.get(ac.id);
  }
  //used to update record
  public void updateAccount(){
      if(ac.Id!=null && ac.Name!=null){
          update ac;
          accountMap.put(ac.id,ac);
          ac=new Account();
      }
  }
  //used to populate picklist on VF page
  public List<SelectOption> getAccounts(){
      return accountPL;
  }
}

VF:
<apex:page id="pg" controller="TestClass2">
    <apex:form id="form">
        <apex:pageblock id="pb1">
            Accounts: 
            <apex:selectList value="{!ac.Id}" size="1" multiselect="false" onchange="actnFn()">
                <apex:selectoptions value="{!Accounts}"/>
            </apex:selectList>
            <apex:actionFunction id="actnFn" name="actnFn" action="{!showValues}" rerender="pb1,pb2"/>
        </apex:pageblock>
        <apex:pageblock id="pb2" >
            Fax: <apex:inputText value="{!ac.Fax}"/><br/>
            Phone: <apex:inputText value="{!ac.Phone}"/><br/>
            Website: <apex:inputText value="{!ac.Website}"/><br/>
            <apex:commandButton value="Update" action="{!updateAccount}"/>           
        </apex:pageblock>
    </apex:form>
    
</apex:page>


Mark it as Best Answer, if it helps.
Maddy6161Maddy6161
Thanks a lots Anil kumar Gorantala, your code is so help full, I am new on this technlogy so am getting problem, i have added inline edit but when i pressed saved button its not save, can u help me on this
Maddy6161Maddy6161
Thanks now its working good 
thanks Srinivasa Reddy S,Anil kumar Gorantala
Srinivas SSrinivas S
Code to save the record after editing the fiels, tested it is saving also.

Controller Class -
public with sharing class AccDropdownRelFieldsContrl {
    public String selVal {get;set;}
    Map<String,Account> accNameAccsMap = new Map<String,Account>();
    public List<SelectOption> accountNames {get;set;}
    Account accForSave = new Account();
    public AccDropdownRelFieldsContrl() {
        selVal = '--None--';   
        for(Account acc : [select Name, Fax, Phone, Website from Account limit: Limits.getLimitQueryRows()]) {
            accNameAccsMap.put(acc.Name,acc);
        }
        accountNames = new List<SelectOption>();
        accountNames.add(new SelectOption('--None--','--None--'));
        for(String accName : accNameAccsMap.keyset()) {
            accountNames.add(new SelectOption(accName,accName));
        }
    }
    
    public Account getAccount() {
        if(accNameAccsMap.containsKey(selVal)) {
            accForSave = accNameAccsMap.get(selVal);
            return accNameAccsMap.get(selVal);
        }
        
        else
            return new Account();
        
    }
    
    public void save() {
        upsert accForSave;
    }
}

Visualforce Page -
<apex:page controller="AccDropdownRelFieldsContrl" tabStyle="Account">
    <apex:form >
        <apex:pageBlock mode="Edit" Title="Account">
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account"/>
                    <apex:selectList size="1" value="{!selVal}">
                        <apex:selectOptions value="{!accountNames}"/>
                        <apex:actionSupport event="onchange" reRender="accInfoSec"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:outputPanel id="accInfoSec">            
                <apex:pageblockSection title="{!account.Name}" rendered="{!selVal != '--None--'}">
                    <apex:inputField value="{!account.Fax}"/>
                    <apex:inputField value="{!account.Phone}"/>
                    <apex:inputField value="{!account.Website}"/>
                </apex:pageblockSection>
                <apex:panelGrid rendered="{!selVal != '--None--'}" style="margin-left:50%">
                    <apex:commandButton value="Save"/>
                </apex:panelGrid>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
Anil kumar GorantalaAnil kumar Gorantala
Hi srinivas, the values are not changing when account is changed please check the code
Srinivas SSrinivas S
Hi Anil,

Thaks for letting me know that it is not working.

I have found following issues -
1. Forgot to call 'Save' action from CommandButton.
2. For some reason inputFields are not rerendering, while troubleshooting I have figured out that -
  • Page is displaying only few inputfields, few fields on the account are not displaying those fields might be the mandatory fields.
  • Due to this reason inputfields are not rerendering.
  • To resolve this issues, while invoking method through unchange event inputFields should not be processed at server side.
  • I wrapper selectlist in 'actionregion', this resolve the issue of rerendering of inputfields
Finally, everything working well, please let me know if you have any more suggestions.

Controller Class -
public with sharing class AccDropdownRelFieldsContrl {
    public String selVal {get;set;}
    Map<String,Account> accNameAccsMap = new Map<String,Account>();
    public List<SelectOption> accountNames {get;set;}
    public AccDropdownRelFieldsContrl() {
        selVal = '--None--';   
        for(Account acc : [select Name, Fax, Phone, Website from Account limit: Limits.getLimitQueryRows()]) {
            accNameAccsMap.put(acc.Name,acc);
        }
        accountNames = new List<SelectOption>();
        accountNames.add(new SelectOption('--None--','--None--'));
        for(String accName : accNameAccsMap.keyset()) {
            accountNames.add(new SelectOption(accName,accName));
        }
    }
    
    public Account getAccount() {
        if(accNameAccsMap.containsKey(selVal)) {
            return accNameAccsMap.get(selVal);
        }
        
        else
            return new Account();
        
    }
    
    public void save() {
        upsert accNameAccsMap.get(selVal);
    }
}

Visualforce Page -
<apex:page controller="AccDropdownRelFieldsContrl" tabStyle="Account">
    <apex:pagemessages />
    <apex:form >
        <apex:pageBlock mode="Edit" Title="Account">
            <apex:pageBlockSection >            
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account"/>
                    <apex:actionRegion >
                        <apex:selectList size="1" value="{!selVal}">
                            <apex:selectOptions value="{!accountNames}"/>
                            <apex:actionSupport event="onchange" reRender="accInfoSec"/>
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>                
            </apex:pageBlockSection>
            <apex:outputPanel id="accInfoSec">            
                <apex:pageblockSection title="{!account.Name}" rendered="{!selVal != '--None--'}" id="accPbSec">
                    <apex:inputField value="{!account.Fax}"/>
                    <apex:inputField value="{!account.Phone}"/>
                    <apex:inputField value="{!account.Website}"/>
                </apex:pageblockSection>
                <apex:panelGrid rendered="{!selVal != '--None--'}" style="margin-left:50%">
                    <apex:commandButton value="Save" action="{!Save}" rerender="accInfoSec"/>
                </apex:panelGrid>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>


------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
Anil kumar GorantalaAnil kumar Gorantala
we can implement inline edit also for this. that one would be great. 
Maddy6161Maddy6161
Yes now inline edit working Thanks
Srinivas SSrinivas S
Maddy, if it is woking can you please mark it as a solution, so that it will be helpful for others.

Thanks,
Srinivas
Anil kumar GorantalaAnil kumar Gorantala
With Inline editing:
<apex:page standardController="account" extensions="accountcontroller" >
    <apex:form >
        <apex:pageBlock title="Display Account Details: " mode="inlineEdit">
            <apex:pageBlockButtons id="buttons">
                <apex:commandButton action="{!edit}" id="editButton" value="Edit"/>
                <apex:commandButton action="{!save}" id="saveButton" value="Save"/>
                <apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
            <apex:outputLabel value="Account Name: " />
            <apex:selectlist value="{!accid}" size="1" >
                <apex:selectOption itemLabel="None" itemValue="None"></apex:selectOption>
                <apex:selectOptions value="{!accountnames}" />
                <apex:actionSupport action="{!getDetails}" event="onchange" rerender="display"/>
            </apex:selectlist>
            </apex:pageBlockSection>
        <apex:pageBlockSection id="display" title="Account Details">
            <apex:outputField value="{!SelectedAccount.Phone}">
            <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
            </apex:outputField>
            <apex:outputField value="{!SelectedAccount.fax}">
            <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
            </apex:outputField>
            <apex:outputField value="{!SelectedAccount.Website}">
            <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
            </apex:outputField>
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class accountcontroller {

    public string accid {get; set;}
    public Account SelectedAccount{get;set;}
    public list<selectoption> accoptions{get; set;}
    public accountcontroller(ApexPages.StandardController controller){
        
    }
    
    public list<selectoption> getaccountnames() {
        accoptions = new list<selectoption>();
        for (account acc : [select id, name from account]){
            accoptions.add(new selectoption(acc.id, acc.name));
        }  
        return accoptions;
    } 
    
    public void  getDetails() {
        SelectedAccount = new Account();
        if(accid!='None')
        SelectedAccount = [SELECT id, Name, Phone,fax,website FROM Account WHERE id=:accid ];
        
    }
}