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

how to pass argument to apex method in controller from JavaScript
Hi Experts,
Can u pls let me know how to pass argument from my VF page to controller, using javaScript?
My VF page
<apex:page controller="ViewQAAMController">
<apex:form >
<apex:actionFunction action="{!callMe}" name="callthisinJavaScript" reRender="a" />
<apex:pageBlock >
<apex:pageBlockTable value="{!BadgeList}" var="data" align="center" styleClass="table table-striped" >
<apex:column headerValue="MyValues">
<apex:outputLink value="{!data['Week__c']}" onclick="openWindow()">{!data['Week__c']}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
<script>
function openWindow(){
window.open('/apex/AddQAAMWeeklyPlanner');
alert(callthisinJavaScript());
callthisinJavaScript();
}
</script>
</apex:page>
My controller:
public class ViewQAAMController {
public PageReference MyMethod() {
System.debug('I am called');
pageReference pg = new pageReference('http://www.google.com/');
pg.setRedirect(true);
return pg;
}
public void callMe()
{
System.debug('I am called by JavaScript');
}
//public List<AggregateResult> BadgeList { get; set; }
public List<AggregateResult> BadgeList
{
get
{
List<AggregateResult> badges = [select Week__c, max(createddate) apple from QAAM_Weekly_Planner__c Max group by Week__c order by max(createddate) desc LIMIT 10];
return badges;
}
set;
}
}
I wanted to pass value: {!data['Week__c']} to my controller from JavaScript, pls let me know how to achieve this?
Regards
Karthic Sankar V P
Can u pls let me know how to pass argument from my VF page to controller, using javaScript?
My VF page
<apex:page controller="ViewQAAMController">
<apex:form >
<apex:actionFunction action="{!callMe}" name="callthisinJavaScript" reRender="a" />
<apex:pageBlock >
<apex:pageBlockTable value="{!BadgeList}" var="data" align="center" styleClass="table table-striped" >
<apex:column headerValue="MyValues">
<apex:outputLink value="{!data['Week__c']}" onclick="openWindow()">{!data['Week__c']}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
<script>
function openWindow(){
window.open('/apex/AddQAAMWeeklyPlanner');
alert(callthisinJavaScript());
callthisinJavaScript();
}
</script>
</apex:page>
My controller:
public class ViewQAAMController {
public PageReference MyMethod() {
System.debug('I am called');
pageReference pg = new pageReference('http://www.google.com/');
pg.setRedirect(true);
return pg;
}
public void callMe()
{
System.debug('I am called by JavaScript');
}
//public List<AggregateResult> BadgeList { get; set; }
public List<AggregateResult> BadgeList
{
get
{
List<AggregateResult> badges = [select Week__c, max(createddate) apple from QAAM_Weekly_Planner__c Max group by Week__c order by max(createddate) desc LIMIT 10];
return badges;
}
set;
}
}
I wanted to pass value: {!data['Week__c']} to my controller from JavaScript, pls let me know how to achieve this?
Regards
Karthic Sankar V P
Do below changes in VF page and controller-
Chage 1 - VF Page
Add an id attrbute to all the VF tags leading towards <apex:outputLink> like below-
Change 2 - Inside controller, simply traverse from <apex:page> to <apex:outputLink> to obtain the value - Let me know if this helps!
I can do that, but how that value is passed to the controller? its still in my JavaScript block right? please correct me if my understanding is wrong here
Regards
Karthic Sankar
Use <apex:param> inside <apex:outputLink> to pass valur from VF to Apex- Refer this one - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_param.htm
Hope this helps!
Thanks for your reply, I tired but, I am getting value as NULL.
22:05:26:018 USER_DEBUG [12]|DEBUG|I am called by JavaScriptnull
MY Contoller
public class ViewQAAMController {
public PageReference MyMethod() {
System.debug('I am called');
pageReference pg = new pageReference('http://www.google.com/');
pg.setRedirect(true);
return pg;
}
public void callMe()
{
System.debug('I am called by JavaScript' + ValueFromVF);
}
public String valueFromVF { get; set; }
//valueFromVF
public List<AggregateResult> BadgeList
{
get
{
List<AggregateResult> badges = [select Week__c, max(createddate) apple from QAAM_Weekly_Planner__c Max group by Week__c order by max(createddate) desc LIMIT 10];
return badges;
}
set;
}
}
My VF page:
<apex:page controller="ViewQAAMController">
<apex:form >
<apex:actionFunction action="{!callMe}" name="callthisinJavaScript" reRender="a" />
<apex:pageBlock >
<apex:pageBlockTable value="{!BadgeList}" var="data" align="center" styleClass="table table-striped" >
<apex:column headerValue="MyValues">
<apex:outputLink value="{!data['Week__c']}" onclick="openWindow()">{!data['Week__c']}</apex:outputLink>
<apex:param name="weekValue" value="{!data['Week__c']}" assignTo="{!valueFromVF}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
<script>
function openWindow(){
window.open('/apex/AddQAAMWeeklyPlanner');
alert(callthisinJavaScript());
callthisinJavaScript();
}
</script>
</apex:page>
Please help, i have underlined the changes with Bold.
Regards
Karthic
I request anyone to please help?
I am stuck here.
Reagrds
Karthic
If you see the snippet I pasted, <apex:param> must be within <apex:outputLink>
Try changing accordingly, and let me know the result!
My bad Sorry.. I have tried as suggested, Still I am getting null value, please help.
Controller:
public class ViewQAAMController {
//valueFromVF
public String valueFromVF { get; set; }
public List<AggregateResult> BadgeList
{
get
{
List<AggregateResult> badges = [select Week__c, max(createddate) apple from QAAM_Weekly_Planner__c Max group by Week__c order by max(createddate) desc LIMIT 10];
return badges;
}
set;
}
public void CallThisMethod()
{
System.debug('The value from VF is '+valueFromVF);
}
}
Visual Force
<apex:page controller="ViewQAAMController">
<apex:form >
<apex:actionFunction action="{!CallThisMethod}" name="callthisinJavaScript" reRender="a" />
<apex:pageBlock >
<div align="center" draggable="false" >
<apex:commandButton value="New" onclick="openQAAM()" disabled="false"/>
</div>
<apex:pageBlockTable value="{!BadgeList}" var="data" align="center" styleClass="table table-striped" >
<apex:column headerValue="MyValues">
<apex:outputLink id="PBValue" value="{!data['Week__c']}" onclick="javascript:openWindow('{!$Component.PBValue}')">
<apex:param name="weekValue" value="{!data['Week__c']}" assignTo="{!valueFromVF}"/>
{!data['Week__c']}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
<script>
function openWindow(Arg){
var str = document.getElementById('{!$Component.PBValue}');
alert(str);
window.open('/apex/AddQAAMWeeklyPlanner');
callthisinJavaScript();
}
function openQAAM()
{
window.open('/apex/AddQAAMWeeklyPlanner');
}
</script>
</apex:page>