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
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 ];
        
    }
}

 
root alexroot alex
Yes, it is possible to create a Visualforce (VF) form with a dropdown field for account names, and based on the selected account, show related fields. You can achieve this using a combination of Visualforce, Apex, and JavaScript.
Here's an example of how you can accomplish this:

Create a Visualforce page:
<apex:page controller="AccountController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:outputLabel value="Select an Account:"/>
                <apex:selectList value="{!selectedAccountId}" size="1">
                    <apex:selectOptions value="{!accountOptions}"/>
                    <apex:actionSupport event="onchange" action="{!getRelatedFields}" rerender="relatedFieldsSection"/>
                </apex:selectList>
            </apex:pageBlockSection>
            <apex:pageBlockSection id="relatedFieldsSection" rendered="{!showRelatedFields}">
                <!-- Display your related fields here -->
                <apex:outputText value="Related Field 1: {!relatedField1}"/>
                <apex:outputText value="Related Field 2: {!relatedField2}"/>
                <!-- Add more related fields as needed -->
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Create an Apex controller (AccountController) to handle the logic:
public class AccountController {
    public String selectedAccountId { get; set; }
    public Boolean showRelatedFields { get; set; }
    public String relatedField1 { get; set; }
    public String relatedField2 { get; set; }
    // Add more related fields as needed

    public List<SelectOption> getAccountOptions() {
        List<SelectOption> options = new List<SelectOption>();
        for (Account acc : [SELECT Id, Name FROM Account]) {
            options.add(new SelectOption(acc.Id, acc.Name));
        }
        return options;
    }

    public void getRelatedFields() {
        if (selectedAccountId != null) {
            Account selectedAccount = [SELECT Related_Field_1__c, Related_Field_2__c FROM Account WHERE Id = :selectedAccountId LIMIT 1];
            relatedField1 = selectedAccount.Related_Field_1__c;
            relatedField2 = selectedAccount.Related_Field_2__c;
            // Fetch and assign values to more related fields as needed
            showRelatedFields = true;
        } else {
            showRelatedFields = false;
        }
    }
}

In this example, the Visualforce page uses the apex:selectList component to display a dropdown list of account names. The apex:actionSupport component triggers an action in the Apex controller when the selected value changes.

The getAccountOptions method in the Apex controller retrieves the account names and populates the dropdown list.

The getRelatedFields method is invoked when the selected account changes. It fetches the related fields from the selected account and assigns them to variables. The rendered attribute is used to conditionally display the related fields section based on whether an account is selected.

You can customize the Visualforce page and Apex controller code according to your specific requirements, including adding more related fields and modifying the query to fetch the account data.

Note: Make sure to handle error scenarios, such as when an account is not found or when there are no related fields available for a selected account. Source https://fiverrme.com/