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
karthic sankar 9karthic sankar 9 

how to call apex method from JavaScript in VF

Hi All,

Please help me with below request.

I want to calll a apex method from my javaScript

<apex:page controller="ViewQAAMController">
<apex:form >
<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:actionFunction action="{!MyMethod}" name="myFunction" />
</apex:pageBlock>
</apex:form>
<script>
function openWindow(){
         window.open('/apex/AddQAAMWeeklyPlanner');
         MyFunction();
        }
    </script>
</apex:page>


Controller:

public class ViewQAAMController {

    public PageReference MyMethod() {
            System.debug('I am called ILU');
        return null;
    }


    //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 want to call Mymethod() from controller from Java Script.

Kindly help

Regards
Karthic Sankar V P
Best Answer chosen by karthic sankar 9
Mahendra Singh 19Mahendra Singh 19
<apex: page>
<apex:form id ="frm">
<apex:actionfunction name="callfromJS"  action="{!controllerMethodName} reRender="frm"/>
 
</apex:form>
<script>
function JSmethodCallFromAnyAction()
{
callfromJS();
}
</apex:page>

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi,

Your query is answered in the below blogs,

https://developer.salesforce.com/forums/?id=906F00000008zaMIAQ

https://salesforce.stackexchange.com/questions/117978/calling-an-apex-method-from-javascript

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Mahendra Singh 19Mahendra Singh 19
<apex: page>
<apex:form id ="frm">
<apex:actionfunction name="callfromJS"  action="{!controllerMethodName} reRender="frm"/>
 
</apex:form>
<script>
function JSmethodCallFromAnyAction()
{
callfromJS();
}
</apex:page>
This was selected as the best answer
karthic sankar 9karthic sankar 9
Thank you Both. You are awesome
karthic sankar 9karthic sankar 9
But there is one more question, how to pass parameter to the controller, like argument?
Mahendra Singh 19Mahendra Singh 19

Hi Kartik,

Use below sample code : 
<apex:actionFunction action="{!method1}" name="callMethod1" rerender="formId" >
        <apex:param assignTo="{!param1}" name="prmNm" value=""/>
    </apex:actionFunction>

Apex Controller :

public String param1{get;set;}

Please mark it as best answer