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,

 

I want to populate one text field values depending on other text field values present on same page.

I have tried this but when i put the hard coded value in the soql query its working fine and when i use variable instead of hard coded value it throws exception.I have mentioned my code below please guide me to achieve desired functionality.

 

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

 Code which is underlined generates this 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

 


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

Edwin VijayEdwin Vijay

The reason is:

 

When your page loads initially the query would be executed. When the page loads there is no value in the textbox and the variable fName.

 

Put your query inside a method and call it when some action happens in your page after entering a value in the inputbox..

 

Thanks