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
RavisankarRavisankar 

Passing an HTML input to Apex

Hi

 

I built a Visualforce page for Account object having Auto complete functionality.

 

I am not able to save the record since am not able to read the html search field(used for account name)..  below is the visualforce page and controller..

<apex:page title="New Account" standardController="Account" extensions="Accounteditpage1" tabStyle="Account" id="pgId">
<apex:includeScript value="{!URLFOR($Resource.jQuery_UI_1_10_3, 'js/jquery-1.9.1.js')}"/> 
    <script src="/soap/ajax/26.0/connection.js" type="text/javascript"></script>
    <apex:includeScript value="{!URLFOR($Resource.jQuery_UI_1_10_3, 'js/jquery-ui-1.10.3.custom.js')}"/>
    <apex:stylesheet value="{!URLFOR($Resource.jQuery_UI_1_10_3, 'css/ui-lightness/jquery-ui-1.10.3.custom.css')}"/>
    
<apex:sectionHeader id="mainHeader" title="New Account"/>
<style type="text/css">
span.reqField{
    background-color:#CC0000;
    margin: 3px; 0px; 0px; 0px; 
    padding-top: 0px;  
    padding-right: 0px;  
    padding-bottom: 0px; 
    padding-left: 0px; 
}
</style>
<apex:detail />
<apex:form id="f" >
    
    <apex:pageMessages id="info"></apex:pageMessages>
    <apex:pageBlock id="AccountDetailBlock" title="Account Edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" onclick="setHidden();" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
                <apex:commandButton value="Save & New"/>
            </apex:pageBlockButtons>    
     
            <apex:pageBlockSection title="Account Information">  
            <apex:pageblockSectionItem >

                <b>Account Name(Display list of accounts)</b><input type="text" id="sfjscontactautocomplete"/> 
                <apex:inputHidden id="hdnRep2" value="{!sfjscontactautocomplete}"/> 
                    </apex:pageblockSectionItem>                                             
                <apex:repeat value="{!$ObjectType.Account.FieldSets.GVAccountinformation}" var="f">  
                
                <apex:inputfield value="{!Account[f]}" required="{!OR(f.required, f.dbrequired)}"/>  
                </apex:repeat>
            </apex:pageBlockSection>
               <apex:pageBlockSection title="Address Information">
                
                <apex:repeat value="{!$ObjectType.Account.FieldSets.GVaddressinformation}" var="f">  
                <apex:inputfield value="{!Account[f]}" required="{!OR(f.required, f.dbrequired)}"/>  
                </apex:repeat>
            
            
               </apex:pageBlockSection>
                             
                <apex:pageBlockSection title="Additional Information">
                
                        <apex:repeat value="{!$ObjectType.Account.FieldSets.GVadditionalinformation}" var="f">  
                        <apex:inputfield value="{!Account[f]}" required="{!OR(f.required, f.dbrequired)}"/>  
                        </apex:repeat>
            
            
               </apex:pageBlockSection>
                <apex:pageBlockSection title="Financial Information">
                
                        <apex:repeat value="{!$ObjectType.Account.FieldSets.GVfinancialinformation}" var="f">  
                        <apex:inputfield value="{!Account[f]}" required="{!OR(f.required, f.dbrequired)}"/>  
                        </apex:repeat>
            
            
               </apex:pageBlockSection>
                <apex:pageBlockSection title="Spend &amp; Competitor Information">
                
                        <apex:repeat value="{!$ObjectType.Account.FieldSets.GVSpend_ampCompetitorInformation}" var="f">  
                        <apex:inputfield value="{!Account[f]}" required="{!OR(f.required, f.dbrequired)}"/>  
                        </apex:repeat>
            
            
               </apex:pageBlockSection>   
                <apex:pageBlockSection title="Virads Integration Status">
                
                        <apex:repeat value="{!$ObjectType.Account.FieldSets.GVViradsIntegrationStatus}" var="f">  
                        <apex:inputfield value="{!Account[f]}" required="{!OR(f.required, f.dbrequired)}"/>  
                        </apex:repeat>
            
            
               </apex:pageBlockSection>  
                <apex:pageBlockSection title="System Information">
                
                        <apex:repeat value="{!$ObjectType.Account.FieldSets.GVsysteminformation}" var="f">  
                        <apex:inputfield value="{!Account[f]}" required="{!OR(f.required, f.dbrequired)}"/>  
                        </apex:repeat>
            
            
               </apex:pageBlockSection>                 
                                                                  
            
     </apex:pageBlock>
    
</apex:form>
<script>
        var j = jQuery.noConflict();      
        //We will establish a connection salesforce database using the sforce.connection.init(sessionID, ServerURL) function.
        var sid = '{!$Api.Session_ID}';
        var server = "https://" + window.location.host + "/services/Soap/u/26.0";
        sforce.connection.init(sid, server);
         
        //We will query the Account object using the sforce.connection.query function. This will return 200 results.
        var result = sforce.connection.query("select Name from Account");
        var records = result.getArray("records");
        var javascriptAccountList =[];
         
        //Iterate thru the list of Accountand store them in a javascript simple array variable which will then assign it to the source of the autocomplete.
        for(i=0;i<records.length;i++){
            javascriptAccountList[i]=records[i].Name;
        }
        //on Document ready
        j(document).ready(function(){
             
                j("#sfjscontactautocomplete").autocomplete({
                source : javascriptAccountList
            });
        });
        
        function setHidden()
    {
        var hiddenRep = document.getElementById('pgId:f:AccountDetailBlock:hdnRep2');
        hiddenRep.value = document.getElementById('sfjscontactautocomplete').value;
    }
    </script>
</apex:page>

 

public with sharing class Accounteditpage1 {
Account Acc;
    public Accounteditpage1(ApexPages.StandardController controller) {

    }
    public String sfjscontactautocomplete{get;set;}
    public pagereference save(){
    
     new account().addError('*****'+ sfjscontactautocomplete);

return null;

}

}

 I am getting an error message which I used in the controller. please help me with this.

Puja_mfsiPuja_mfsi

HI,

Please check the id of your inputHidden.go to the inspect element and check the id value of inputhidden.

you have define apex:inputHidden inside the pageblock,pageblocksection and pageBlockSectionItem and in javascript u have only give the reference of pageBlock.

 

 <!-- you need to put the ids of pageBlockSection and pageBlocksectionItem As well. -->

<apex:pageBlock id="AccountDetailBlock" title="Account Edit">

       <apex:pageBlockSection title="Account Information" id="accSection"> 

               <apex:pageblockSectionItem  id="accItem">

                          <apex:inputHidden id="hdnRep2" value="{!sfjscontactautocomplete}"/>

                  </apex:pageblockSectionItem>  

 

Now in java script you need to add :

 

function setHidden()

{
var hiddenRep = document.getElementById('pgId:f:AccountDetailBlock:accSection:accItem:hdnRep2');
hiddenRep.value = document.getElementById('sfjscontactautocomplete').value;
}

 

Please let me know if u have any problem on same and if this post helps u plz give KUDOS by clik on star at left.

RavisankarRavisankar

Hi

 

Thanks for your reply.

 

I updated the code as you mentioned but I am not able to save the record after hitting "Save" button. it is staying on the same page no error messages.

 

Please find updated code below

 

function setHidden()
{
var hiddenRep = document.getElementById('pgId:f:AccountDetailBlock:accSection:accItem:hdnRep2');
hiddenRep.value = document.getElementById('sfjscontactautocomplete').value;
}