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
RAJU_DEEPRAJU_DEEP 

Population of one field by selection of another field in the same page

Hello Team.

 

           My requirement is "i want to populate one text field value in visualforce page by selecting another field(by clicking on enter key) in the same page".

 

Scenario :

 

1. I have three text fields in my page Employee Id, Employee FName, Employee LName.

2. Whenever user will put Employee Id and press enter key  on that field(Employee Id) the related data should be populated on another two fields(Employee FName, Employee LName) .

 

Please guide me how can i achieve this functionality.

 

Thanks & regards,

Raju Deep

SurekaSureka

Hi,

 

My assumption is that Employee FName and LName already exists along with the Id. If so, you can try with the below sample code.

 

 

<apex:page standardController="Account" extensions="testpage">
<apex:form >
<apex:inputText value="{!accountName}"/>
<apex:commandButton value="Enter" action="{!rerendervalues}"/>
<apex:outputText value="{!accountId}" rendered="{!bool}"/>
</apex:form>
</apex:page>

Apex Class:


public string accountId{get;set;}
public string accountName{get;set;}

public pagereference rerendervalues() {
a = [select Id from Account where Name=:accountName];
accountId = a.Id;
bool= true; return null; }

 In the above example, when you enter the Account Name and click on the Enter Button, AccountId will be rendered. Similarly you can modify the code to display the FName and LName.

 

Thanks

 

eworks123456eworks123456

Hi Surekha,

 

Thanks for your quick response, I try this on my side then i let you know whether its working or not.

 

Regards,

Raju deep

RAJU_DEEPRAJU_DEEP

Thanks for your reply I tried your code in my side its working fine but it was on the button click and as per my requirement it should work on text field onclick event.

 

The code which I am trying is given below:

 

Visualforce Page Code:

 

<apex:page controller="ActionPageController">
    <apex:form >
        <apex:actionFunction name="hitMe" action="{!iWantMyJSValues}" rerender="jsvalues">
            <apex:param name="one" value="" />
            <apex:param name="two" value="" />
            <apex:param name="three" value="" />
        </apex:actionFunction>
        
        <apex:outputPanel id="jsvalues">
            Value one:<apex:inputText value="{!fName}" onclick="hitMe('{!fName}', '{!$User.FirstName}', '{!emp1}')"/><br/>
            Value two:<apex:inputText value="{!valueTwo}" /><br />   
            Employee Name:<apex:inputText value="{!valueThree}" /><br />
            Employee:<apex:inputText value="{!valueOne}" /><br />           
        </apex:outputPanel>
        <!--<span style="cursor: pointer;" 
            onclick="hitMe(Date(), '{!$user.FirstName}', '{!emp1}')">Hit Me</span>-->
    </apex:form>
</apex:page>

 

 

Controller Code:

 

 

public with sharing class ActionPageController {

    public String valueOne { get; set; }
    public String valueTwo { get; set; }
    public String valueThree { get; set; }
    public String fName { get; set; }
    
    public PageReference iWantMyJSValues() {
        valueOne = Apexpages.currentPage().getParameters().get('one');
        valueTwo = Apexpages.currentPage().getParameters().get('two');
        valueThree = Apexpages.currentPage().getParameters().get('three');
        
        return null;
    }
    String emp1 = [select Last_Name__c from Employee_Detail__c where name =: fName].Last_Name__c;
  
  public String getEmp1(){
        return emp1;
    }
}

 The above code generates following exception:

 

 

System.QueryException: List has no rows for assignment to SObject

Class.demoPackageTest.ActionPageController: line 20, column 19 Class.demoPackageTest.ActionPageController: line 1, column 27

 

Code which is underlined generates this exception, actually i am trying to build dynamic query when i give hard coded values in place of dynamic variable its working fine. Please guide me so that i could remove this exception and make my query dynamic .

 

Regards,

Raju Deep

SurekaSureka

Hi,

I guess, the variable "fName" is not having any value. Try printing that variable using system.debug.

If so, give the emp1 query inside the method "iWantMyJSValues".

Please let me know, if that works.

Thanks