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
stollmeyerastollmeyera 

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?

Best Answer chosen by Admin (Salesforce Developers) 
Saurabh DhobleSaurabh Dhoble

I think it's your isInConsole() method - try printing it's value somwhere.

This works for me :-

 

<apex:page controller="Js_Test_Class">
    <apex:form>
        <script>
        	function setVal()
            {
            	document.getElementById("{!$Component.hdnField}").value = "TestValue";
            }
        </script>
        <apex:inputHidden id="hdnField" value="{!theValue}" />
        <apex:commandButton value="Post Page" action="{!post}" />
    </apex:form>
    <script>
        setVal();
    </script>
</apex:page>

 

public class Js_Test_Class {
	public String theValue
    {
        get;
        //{
            //if(theValue == null)
            //    theValue = '';
            //return theValue;  
        //}
        set;
    }
    
    public void post()
    {
        System.Debug('The value is : ' + theValue);
    }
}

 

All Answers

Saurabh DhobleSaurabh Dhoble

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 :-

document.getElementById("{$Component.pageBlockId.pageBlockSectionId.hiddenFieldId}");

 Let me know if this works.

stollmeyerastollmeyera

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.

 

<apex:page standardController="Opportunity" title="Change Owner" extensions="ChangeOppOwner">
    <apex:PageMessages />
    
    <apex:form >
    
            <!-- PASS JAVASCRIPT VARIABLE !-->
            <script type="text/javascript" >
Var con = sforce.console.isInConsole(); document.getElementById('{!$Component.Console}').value = con; </script> <apex:inputHidden id="Console" value="{!ConsoleVal}" />

<!-- START OF VF PAGE !-->
<apex:pageblock >

 
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.  

Saurabh DhobleSaurabh Dhoble

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 ?

stollmeyerastollmeyera

They do indeed match.

 

 

  con = sforce.console.isInConsole();  
document.getElementById('j_id0:j_id28:Console').value = con;

and 

 

<input id="j_id0:j_id28:Console" type="hidden" name="j_id0:j_id28:Console" >

 

 

Saurabh DhobleSaurabh Dhoble

I think it's your isInConsole() method - try printing it's value somwhere.

This works for me :-

 

<apex:page controller="Js_Test_Class">
    <apex:form>
        <script>
        	function setVal()
            {
            	document.getElementById("{!$Component.hdnField}").value = "TestValue";
            }
        </script>
        <apex:inputHidden id="hdnField" value="{!theValue}" />
        <apex:commandButton value="Post Page" action="{!post}" />
    </apex:form>
    <script>
        setVal();
    </script>
</apex:page>

 

public class Js_Test_Class {
	public String theValue
    {
        get;
        //{
            //if(theValue == null)
            //    theValue = '';
            //return theValue;  
        //}
        set;
    }
    
    public void post()
    {
        System.Debug('The value is : ' + theValue);
    }
}

 

This was selected as the best answer
stollmeyerastollmeyera

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!

DevelopersDevelopers

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

 

 

stollmeyerastollmeyera

I used the getElementId method, which pulls a value of "undefined" when youre using the Console:

 

    <apex:form >
    <apex:inputHidden id="GetTop" value="{!gettopval}" />
            
        <script type="text/javascript" >
        
            function setVal(){
                var gettop = top.location.href;
                document.getElementById('{!$Component.GetTop}').value = gettop;
            }
                
        </script>
        

  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.  

 

        //IF UNDEFINED SEND TO CONSOLE
if(opps.size() > 0 && gettopval == 'undefined'){ System.debug('Look here--->' + gettopval); PageReference Reactivate= new PageReference('/' + opps[0].id + '?isdtp=mn&retURL=/ui/desktop/DesktopMainDefaultPage?isdtp=mn'); Reactivate.setRedirect(true); return Reactivate; }
//OTHERWISE SEND TO OBJECT else{ System.debug('Look here--->' + gettopval); PageReference Reactivate= new PageReference('/' + opps[0].id); Reactivate.setRedirect(true); return Reactivate; }

 

Hope this helps!

Rocks_SFDCRocks_SFDC

 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