You need to sign in to do that
Don't have an account?
Passing Javascript variable to Apex Class
I've looked around for a couple hours trying to figure this out but have been unable. I am trying to pass a variable from javascript to my VF extension. The exact variable is 'sforce.console.isInConsole()' to determine if my users are accessing the page via the Console or not. Below is the javascript I have included within my VF page:
<script type="text/javascript" > document.getElementById('{!$Component.Console}').value = sforce.console.isInConsole(); </script>
Below is the inputHidden field:
<apex:inputHidden id="Console" value="{!ConsoleVal}" />
Lastly, below is what I have included within my APEX class:
public String ConsoleVal {get; set;}
All resources I have found lead me to believe that this should work. However, the ConsoleVal variable is null when I look at the debug logs, so there is a disconnect... What am I missing?
I think it's your isInConsole() method - try printing it's value somwhere.
This works for me :-
All Answers
Check if the hidden field is under a pageBlock / pageBlockSection, because the ID is changed in those cases. So if your <apex:inputHidden> is in the structure pageBlock --> pageBlockSection --> hidden field, then the way you it's ID in javascript is :-
Let me know if this works.
Thanks, Saurabh. That makes perfect sense. However, I gave it a shot and the variable is still not passing over. Below is an except from my VF page.
Within my class I simply have "public String ConsoleVal {get; set;}", which I attempt to reference in a PageReference, but the value is still blank in the debug logs.
Ok, that's interesting.
Right-click on your page in a browser and choose "View Source". This will show you the actual HTML for the page. Locate your javascript, and check the "document.getElementById()" piece.Also locate the hidden variable in the HTML.
Does the ID used by document.getElementById() match the ID of the hidden variable ?
They do indeed match.
and
I think it's your isInConsole() method - try printing it's value somwhere.
This works for me :-
There is definitely an issue with the sforce.console.isInConsole(), as another test worked perfectly with my current code. SF lists the "sforce.console.isInConsole()" function in their Console documentation, so I will search elsewhere to find troubleshooting.
Thanks for the help!
Hi Stollmeyera,
Have you got the solution for this. I am also facing the same issue. While VFpage loading itself, it should pass the JS values to the extenstion class.
Can you help me how to fix this?
Thanks in advance....
Regards,
kumar
I used the getElementId method, which pulls a value of "undefined" when youre using the Console:
If it was "undefined" then i routed the user back to the console, otherwise I sent them back to the object they were working with.
Hope this helps!
Hi,
we have simar problem, we want to pass JS variable value to extension on page load.
Scenario: When page loaded, we want to get the current location coordinates and pass it to the controller class.
My Code:
<body onload="xyz();">
<script language="javascript" type="text/javascript">
function xyz(){
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(pos){
var hiddenRep1 = document.getElementById('!$Component.page:frmId:hdnRep1');
var hiddenRep2 = document.getElementById('!$Component.page:frmId:hdnRep2');
var latitude=pos.coords.latitude;
var longitude=pos.coords.longitude;
hiddenRep1= latitude;
hiddenRep2= longitude;
alert("Latitude:" +hiddenRep1);
alert("longitude:" +hiddenRep2);
}
</script>
</body>
we want to get the latitude and longitude values into the controller. Please help.
Thanks,
Anil