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
AndreyAndrey 

Problem with JavaScript

Hi all!
I need to pass a value from javascrip (in vf page) to inputField
Here code:
<apex:page standardController="Travel_Expenses__c" extensions="TE_Object" action="{!Parameters}">
    <apex:form >
        <apex:pageBlock title="Travel Information Edit"  mode="edit">
            <apex:pageBlockButtons >
                    <apex:commandButton action="{!Save}" value="Save"/>
                    <apex:commandButton action="{!SaveAndNew}" value="Save & New"/>
                    <apex:commandButton action="{!Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Information" columns="2">
                      <apex:inputField value="{!NewTE.Traveler__c}" id="Traveler" required="true" 
                                       onchange="TakeCountry(this.id);">
                        <apex:actionsupport event="onchange" reRender="CountryName"/>
                    </apex:inputField>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Country" for="CountryName"/>
                    <apex:inputField value="{!NewTE.Country_Name__c}" id="CountryName" />     
                </apex:pageBlockSectionItem>    
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    <script language="JavaScript1.2" src="/js/functions.js"></script>
    <script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>
    <script type="text/javascript">
        function TakeCountry(id)
        {
            try
            {
                sforce.connection.sessionId = '{!$Api.Session_ID}';
                var cval = document.getElementById(id).value;
                if (cval!=null)
                {   
                    country=sforce.connection.query("Select Country_Names__c from Travel_Information__c WHERE name='"+cval+"'");
                    alert('Selected '+country);
                    records = country.getArray("records");
                    c=records[0].Country_Names__c;
                    alert('C '+c);//after this message I have an error
                    countryname=document.getElementById({!$Component.CountryName}).value;
                    alert('done ');
                    countryname.value='country';
                    alert('Country '+countryname.value);

                }
            }
            catch (error)
            {
                alert('Error: '+error.faultstring);
            }
        }
    </script>
    
</apex:page>
I add a comment where script failed (Error: undefined)

Thanks!

Abhinav GuptaAbhinav Gupta
Can you please add more details i.e. what is alerting in Error message. As you are catching it in generic catch block, one cant be sure of what is the issue.

Plus, you are using old Ajax Toolkit, which is deprecated. You should be using JS Remoting now : https://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

Remoting in Apex + VF can get your requirement addressed for sure.