You need to sign in to do that
Don't have an account?

Unable to pass JS values to controller
Hello,
I have a script that gets the lat and long of a user when they initiate a VF page.
I have tried both apex:outputField and apex:inputputField and still unable to to save the lat and long field to the record
Thanks
I have a script that gets the lat and long of a user when they initiate a VF page.
</script> </head> <div id="startLat"> </div> <div id="startLon"> </div> <script> window.onload = function() { var startPos; var geoSuccess = function(position) { startPos = position; document.getElementById('startLat').innerHTML = startPos.coords.latitude; document.getElementById('startLon').innerHTML = startPos.coords.longitude; }; navigator.geolocation.getCurrentPosition(geoSuccess); }; </script>This works great but I am unable to bind it to the custom lat and long fields on the record.
I have tried both apex:outputField and apex:inputputField and still unable to to save the lat and long field to the record
<label style="display:block;margin-top:12px;" for="Latitude__c"><span style="color:red;">*</span>Latitude</label> <apex:inputField style="margin-left:0;" id="startLan" value="{!SourcingAudit.Latitude__c}" /> <label style="display:block;margin-top:12px;" for="Longitude__c"><span style="color:red;">*</span>Longitude</label> <apex:outputField style="margin-left:0;" id="startLon" value="{!SourcingAudit.Longitude__c}" />Le me know what I am doing incorrect or if there is a better option.
Thanks
Don't get into the confusions:
You are using the Output panel so simply use Css Class selector to set the Lat and Lan in your page.
Sample is below :
<apex:page id="pg" standardController="Account" extensions="AccountExtension">
<script>
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
document.getElementsByClassName('Latclass')[0].value =startPos.coords.latitude;
document.getElementsByClassName('Lonclass')[0].value =startPos.coords.longitude;
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
</script>
<apex:form id="inpform">
<apex:pageBlock title="Account Form" id="pb">
<apex:pageblockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageblockButtons>
<apex:outputPanel id="op">
<label style="display:block;margin-top:12px;" for="Name"><span style="color:red;">*</span>Account</label>
<apex:inputField value="{!Account.Name}" id="accNa" styleclass="findclass"/>
<label style="display:block;margin-top:12px;" for="Latitude__c"><span style="color:red;">*</span>Latitude</label>
<apex:inputField value="{!Account.Latitude__c}" id="startLattt" styleclass="Latclass"/>
<label style="display:block;margin-top:12px;" for="Longitude__c"><span style="color:red;">*</span>Longitude</label>
<apex:inputField value="{!Account.Longitude__c}" id="startLonnn" styleclass="Lonclass"/>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
=======================
There is another way for this uiing page block and sections. where you can use the ID selector to set the Lat and Lan :
Sample is below:
<apex:page id="pg" standardController="Account" extensions="AccountExtension">
<script>
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
document.getElementById('{!$Component.inpform.pb.pbs.startLat}').value=startPos.coords.latitude;
document.getElementById('{!$Component.inpform.pb.pbs.startLon}').value=startPos.coords.longitude;
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
</script>
<apex:form id="inpform">
<apex:pageBlock title="Account Form" id="pb">
<apex:pageblockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageblockButtons>
<apex:pageBlockSection title="AccountInformation" id="pbs">
<apex:inputField value="{!Account.Name}" id="accName"/>
<apex:inputField value="{!Account.Phone}"/>
<apex:inputField value="{!Account.Latitude__c}" id="startLat"/>
<apex:inputField value="{!Account.Longitude__c}" id="startLon"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Mark as best answer if it helps !!!!
Thanks,
Maharajan.C
All Answers
document.getElementById('startLat').value = startPos.coords.latitude;
document.getElementById('startLon').value= startPos.coords.longitude;
Thanks,
Maharajan.C
I made the changes as you suggested but I was still unable to get the LAt and long field values into the record.
Also, when I changed teh synatx from innerHtml to value I no longer see the Lat and long when I initiate the VF.
This is fine but not sure if that was the issue.
Thanks
Thank you for you.
Sorry for the late reply:
Please try to use the Class instaed of id selector. It's working for me. i can able to populate the Lat and Longs in UI:
By using i can't able to assign the values . But if i am using the PageBlock, PB Section and InputField inside the apex form then i can set the value using the ID . You refer my Sample code:
Script :
<script>
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
document.getElementsByClassName('Latclass')[0].value =startPos.coords.latitude;
document.getElementsByClassName('Lonclass')[0].value =startPos.coords.longitude;
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
</script>
VF Page :
<label style="display:block;margin-top:12px;" for="Latitude__c"><span style="color:red;">*</span>Latitude</label>
<apex:inputField value="{!Account.Latitude__c}" styleclass="Latclass"/>
<label style="display:block;margin-top:12px;" for="Longitude__c"><span style="color:red;">*</span>Longitude</label>
<apex:inputField value="{!Account.Longitude__c}" styleclass="Lonclass"/>
========================
The below is the Sample code i have tried in my org:
<apex:page id="pg" standardController="Account" extensions="AccountExtension">
<script>
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
document.getElementById('{!$Component.inpform.pb.pbs.startLat}').value=startPos.coords.latitude;
document.getElementById('{!$Component.inpform.pb.pbs.startLon}').value=startPos.coords.longitude;
//alert('@@@ ' + document.getElementsByClassName('findclass')[0].value);
document.getElementsByClassName('Latclass')[0].value =startPos.coords.latitude;
document.getElementsByClassName('Lonclass')[0].value =startPos.coords.longitude;
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
</script>
<apex:form id="inpform">
<apex:pageBlock title="Account Form" id="pb">
<apex:pageblockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageblockButtons>
<apex:pageBlockSection title="AccountInformation" id="pbs">
<apex:inputField value="{!Account.Name}" id="accName"/>
<apex:inputField value="{!Account.Phone}"/>
<apex:inputField value="{!Account.Latitude__c}" id="startLat"/>
<apex:inputField value="{!Account.Longitude__c}" id="startLon"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputPanel id="op">
<label style="display:block;margin-top:12px;" for="Name"><span style="color:red;">*</span>Latitude</label>
<apex:inputField value="{!Account.Name}" id="accNa" styleclass="findclass"/>
<label style="display:block;margin-top:12px;" for="Latitude__c"><span style="color:red;">*</span>Latitude</label>
<apex:inputField value="{!Account.Latitude__c}" id="startLattt" styleclass="Latclass"/>
<label style="display:block;margin-top:12px;" for="Longitude__c"><span style="color:red;">*</span>Longitude</label>
<apex:inputField value="{!Account.Longitude__c}" id="startLonnn" styleclass="Lonclass"/>
</apex:outputPanel>
</apex:form>
</apex:page>
Apex Class:
public class AccountExtension {
public Account acc;
public AccountExtension(ApexPages.StandardController st){
this.acc = (Account) st.getRecord();
}
}
Thanks,
Maharajan.C
Thnak you for the sample code.
Will you explain the why it it nessesay to have in the Page Block and then have in the Output panel?
It just seem reduntant to have the Lat and Long showuing twice on teh page. If I hide the Page Block will the Lat and Long still be calculated?
Thank you for all your help.
M
Don't get into the confusions:
You are using the Output panel so simply use Css Class selector to set the Lat and Lan in your page.
Sample is below :
<apex:page id="pg" standardController="Account" extensions="AccountExtension">
<script>
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
document.getElementsByClassName('Latclass')[0].value =startPos.coords.latitude;
document.getElementsByClassName('Lonclass')[0].value =startPos.coords.longitude;
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
</script>
<apex:form id="inpform">
<apex:pageBlock title="Account Form" id="pb">
<apex:pageblockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageblockButtons>
<apex:outputPanel id="op">
<label style="display:block;margin-top:12px;" for="Name"><span style="color:red;">*</span>Account</label>
<apex:inputField value="{!Account.Name}" id="accNa" styleclass="findclass"/>
<label style="display:block;margin-top:12px;" for="Latitude__c"><span style="color:red;">*</span>Latitude</label>
<apex:inputField value="{!Account.Latitude__c}" id="startLattt" styleclass="Latclass"/>
<label style="display:block;margin-top:12px;" for="Longitude__c"><span style="color:red;">*</span>Longitude</label>
<apex:inputField value="{!Account.Longitude__c}" id="startLonnn" styleclass="Lonclass"/>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
=======================
There is another way for this uiing page block and sections. where you can use the ID selector to set the Lat and Lan :
Sample is below:
<apex:page id="pg" standardController="Account" extensions="AccountExtension">
<script>
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
document.getElementById('{!$Component.inpform.pb.pbs.startLat}').value=startPos.coords.latitude;
document.getElementById('{!$Component.inpform.pb.pbs.startLon}').value=startPos.coords.longitude;
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
</script>
<apex:form id="inpform">
<apex:pageBlock title="Account Form" id="pb">
<apex:pageblockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageblockButtons>
<apex:pageBlockSection title="AccountInformation" id="pbs">
<apex:inputField value="{!Account.Name}" id="accName"/>
<apex:inputField value="{!Account.Phone}"/>
<apex:inputField value="{!Account.Latitude__c}" id="startLat"/>
<apex:inputField value="{!Account.Longitude__c}" id="startLon"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Mark as best answer if it helps !!!!
Thanks,
Maharajan.C