You need to sign in to do that
Don't have an account?
adrissel
Javascript variables not passing to Apex controller
Hey all,
I am going absolutely mad trying to figure this out! I have read tons and tons of articles online about how to pass a variable from Javascript to my Apex Controller...but NONE OF THEM ARE WORKING FOR ME!!
Can someone take a look at my code here and let me know what the heck is going wrong? I am only pasting the pertinent sections for now unless it is deemed that this code is accurate:
Visualforce Page:
Thus far, if I use JavaScript alert() to show me the value of the "supplier" var it shows perfectly. However, it doesn't pass to the controller:
The result of the System.Debug is:
Everything I have read online tells me that the {get;set;} should get the Javascript variable and set it for the controller.
Why isn't this working for me?!?!
I am going absolutely mad trying to figure this out! I have read tons and tons of articles online about how to pass a variable from Javascript to my Apex Controller...but NONE OF THEM ARE WORKING FOR ME!!
Can someone take a look at my code here and let me know what the heck is going wrong? I am only pasting the pertinent sections for now unless it is deemed that this code is accurate:
Visualforce Page:
<apex:page standardController="Case" recordSetVar="Cases" extensions="SIMSQuickCreate"> <head> <script> function createCase(){ var supplier = document.getElementById('{!$Component.form.block.section1.supplier}').value; createFunction(); } </script> </head> <apex:form id="form" > <apex:actionFunction name="createFunction" action="{!createCase}" rerender=""/> <apex:pageBlock id="block" title="SIMS Quick Create" mode="edit"> <apex:pageBlockSection id="section1" title="Contact Information" columns="2"> <apex:inputField id="supplier" value="{!NewCase.Supplier__c}" required="true"> </apex:pageBlockSection> <center><input type="button" value="Create" onclick="createCase();"/></center> </apex:pageBlock> </apex:form> </apex:page>
Thus far, if I use JavaScript alert() to show me the value of the "supplier" var it shows perfectly. However, it doesn't pass to the controller:
public with sharing class SIMSQuickCreate{ ApexPages.StandardSetController setCon; public SIMSQuickCreate (Apexpages.StandardSetController controller) { setCon = controller; } public String supplier {get; set;} public Case c; public Case getNewCase() { if(c == null){ c = new Case(); } return c; } public PageReference createCase() { System.Debug('>>>>>>>>>>>>> HERE IS THE VALUE --> ' + supplier); return null; } }
The result of the System.Debug is:
(116783031)|USER_DEBUG|[82]|DEBUG|>>>>>>>>>>>>> HERE IS THE VALUE --> null
Everything I have read online tells me that the {get;set;} should get the Javascript variable and set it for the controller.
Why isn't this working for me?!?!
Controller :
public with sharing class TestPassJSVariable{
Apexpages.StandardController setCon;
public string interviewdate {get; set;}
public TestPassJSVariable(Apexpages.StandardController controller) {
setCon = controller;
}
public void saveInterviewDate(){
System.Debug('This is the variable --> '+interviewdate);
}
}
VF Page:
<apex:page standardcontroller="Case" extensions="TestPassJSVariable">
<script type="text/javascript">
function doSave(date) {
paraFunction(date.value);
}
</script>
<apex:form id="form">
<apex:actionFunction name="paraFunction" action="{!saveInterviewDate}" rerender="view">
<apex:param id="aname" assignTo="{!interviewdate}" name="interviewDate" value="" />
</apex:actionFunction>
<apex:inputText id="testString"/>
<script> var searchTag = document.getElementById("{!$Component.testString}"); </script>
<apex:commandLink onclick="doSave(searchTag);">
<apex:commandButton value="Save"/>
</apex:commandLink>
</apex:form>
</apex:page>
All being well, whatever you enter into the text box should be passed to the controller.
All Answers
http://salesforceworld.blogspot.com/2011/06/parameter-passing-using.html
Still not working. Here is the code now:
Controller:Visualforce:
This time NOTHING appears in the debug logs for my test.
What's wrong here?
public string interviewdate {get; set;}
and in the visualfirce page pass paramater as follows ..
<apex:param id="aname" assignTo = "{!interviewdate}" name="interviewdate" value="" />
https://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting_example.htm (https://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting_example.htm" target="_blank)
Thanks to all!
Controller :
public with sharing class TestPassJSVariable{
Apexpages.StandardController setCon;
public string interviewdate {get; set;}
public TestPassJSVariable(Apexpages.StandardController controller) {
setCon = controller;
}
public void saveInterviewDate(){
System.Debug('This is the variable --> '+interviewdate);
}
}
VF Page:
<apex:page standardcontroller="Case" extensions="TestPassJSVariable">
<script type="text/javascript">
function doSave(date) {
paraFunction(date.value);
}
</script>
<apex:form id="form">
<apex:actionFunction name="paraFunction" action="{!saveInterviewDate}" rerender="view">
<apex:param id="aname" assignTo="{!interviewdate}" name="interviewDate" value="" />
</apex:actionFunction>
<apex:inputText id="testString"/>
<script> var searchTag = document.getElementById("{!$Component.testString}"); </script>
<apex:commandLink onclick="doSave(searchTag);">
<apex:commandButton value="Save"/>
</apex:commandLink>
</apex:form>
</apex:page>
All being well, whatever you enter into the text box should be passed to the controller.
I wonder if you might be willing to help me with one follow-up question. If I wanted to pass an array of strings from the Javascript function to the controller, how would I format my System.Debug syntax to output both values?
I currently have the following:
See the comment tags for details. This yields the following:
Any ideas?
<apex:param id="param1" assignTo="{!supplierVariable}" name="param1" value="" />
<apex:param id="param2" assignTo="{!ContactPersonVariable}" name="param2" value="" />
or .. you could pass a comma separated list for all your values from the VF Page into single string parameter in the controller and split it into an array there.
The array "data" is what I am wanting to be passed to the controller.
This doesn't bring across the "data" variable at all. When I debug for just that value it appears null. I also tried this on the VF page:
But, that doesn't even allow the page to process. I think only one variable can be entered into the createFunction.
Thoughts?
I'm not familiar with how to split a list of comma separated values...
Controller:
System.Debug results in a null value.
Any clue why? All I'm passing is a comma-separated string. If I just pass a single variable, it works (i.e. supplier, or conPerson).
<apex:actionFunction name="createFunction" action="{!createCase}" rerender="view">
<apex:param id="data" assignTo="{!data}" name="data" value="" />
</apex:actionFunction>
How do I split the values in the controller to be able to reference/use them independently. The following does NOT work and gives me a null object error:
I am missing something else totally obvious, right?
Got the following error:
Looks like it doesn't like the "[" in that position.
So, I can create a comma separated string from within the controller, and it splits perfectly. But I can't use a comma separated string sent from Javascript without getting an "attempt to de-reference a null object" error?