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
Meer SalmanMeer Salman 

Autofill on 'onselect' event

Hi,
I want to autofill one of the text field (Description text box)  when account code is selected. Following is the VFPage and a Controller. I am not getting any of the result when I select the accout code.  This is quite simple I belive, but I dont know where I am wrong. Please help me!
*********VFPage*********
<apex:page standardController="Fin_Journal__c" extensions="Fin_LineManager">
        
    <script src="../../soap/ajax/24.0/connection.js" type="text/javascript"></script>
    <script type="text/javascript">
        function getDescription()
        {    
            var queryresult = sforce.connection.query("SELECT Description__c FROM Code_Combination__c WHERE Name = '" + document.getElementById('{!$Component.MyForm:acc}').value + "'", queryresult);            
            var records = queryresult.getArray('records');
            document.getElementById('{!$Component.MyForm:desc}').value = records [0].Description__c;
        }
    </script>
      
    <apex:form id="MyForm">
         <b>
         <table>
             <tr>
                <td><apex:outputLabel >Account&nbsp;Code: </apex:outputLabel></td>
                <td><apex:inputField id="acc" value="{!newline.Code_Combination__c}" style="width:320px" onselect="{getDescription()}"/></td>
             </tr>
             <tr>   
                <td><apex:outputLabel >Description:&nbsp;&nbsp;&nbsp;&nbsp;</apex:outputLabel></td>
                <td><apex:inputField id="desc"  value="{!newline.Description__c}" style="width:320px"/></td>
             </tr>
             <tr>   
                <td></td>
                <td><apex:commandButton action="{!save}" value="Save" /></td>
             </tr>
         </table>
         </b>                         
    </apex:form>
</apex:page>
*********CONTROLLER*********
public class Fin_LineManager
    public Line__c newLine {get; set;}
   
    public Fin_LineManager(ApexPages.standardController stdn)
    {
       newLine  = new Line__c(Journal__c = ApexPages.currentPage().getParameters().get('id'));
    }   
    
    
    public PageReference Save()
    {            
       insert newLine;
       return null ;        
    }    
}
Regards,
Meer Salman
MANATTWebMANATTWeb

I'd add an alert({!$Component.MyForm:acc}); to the function and see what you are getting. That way, you'll know when you're passing something into the query that returns a description.

 

Perhaps you need to do onBlur instead or onmouseenter.

 

I'm probably missing something here, but some JS debuggin is probably in order.

 

Meer SalmanMeer Salman

I am not getting the results from the query I tried to get and set the values for the textboxes it went fine, the only problem I am facing is in quering from table. I don't know wats wrong in it. I tried the following code as well

 

<apex:page standardController="Fin_Journal__c" extensions="Fin_LineManager">
     <script type="text/javascript">
        var __sfdcSessionId = '{!GETSESSIONID()}';
    </script>
    
    <script src="../../soap/ajax/24.0/connection.js" type="text/javascript"></script>
    <script type="text/javascript">     
    window.onload = setupPage;
    
    function setupPage() 
    {
        var state = {output : document.getElementById('{!$Component.MyForm.desc}'), startTime : new Date().getTime()};
        var callback = { onSuccess: layoutResults, onFailure: queryFailed, source: state};
        sforce.connection.query("Select Description__c From Code_Combination__c Where Name = '0001'", callback);
    }
    function queryFailed(error, source) 
    {
        source.output.innerHTML = "An error has occurred: " + error;
    }
    
    function layoutResults(queryResult, source) 
    {
        if (queryResult.size > 0) 
        {
            var output = "";    
            var records = queryResult.getArray('records');    
            output = records [0].Description__c;
        }
            source.output.innerHTML = output;
    }
  </script>
        
    <apex:form id="MyForm">
         <b>
         <table>
             <tr>
                <td><apex:outputLabel >Account&nbsp;Code: </apex:outputLabel></td>
                <td><apex:inputField id="acc" value="{!newline.Code_Combination__c}" style="width:320px"/></td>
             </tr>
             <tr>   
                <td><apex:outputLabel >Description:&nbsp;&nbsp;&nbsp;&nbsp;</apex:outputLabel></td>
                <td><apex:inputField id="desc"  value="{!newline.Description__c}" style="width:320px"/></td>
             </tr>
             <tr>   
                <td></td>
                <td><apex:commandButton value="Test" action ="setupPage()"/></td>
             </tr>
         </table>
         </b>                         
    </apex:form>
</apex:page>
Of course there is a data for the query I have passed, but why the snippet is not working? 
Regards,
Meer Salman