+ Start a Discussion
jkcjkc 

How to call a apex class from javascript?

I'm trying to change the timeout value from apex using a button in <apex:actionStatus>
apex class:
global class ApexController {
public Integer intMergeTimeout {get; set;}
public void timeoutMerge(){
        //set timeout to 1second
        intMergeTimeout = 500;
    }
}

javascript:
 
<apex:includeScript value="/soap/ajax/21.0/connection.js"/>
        <apex:includeScript value="/soap/ajax/21.0/apex.js"/>
        <script>
        function timeout(){
            sforce.connection.sessionId = "{!$Api.Session_ID}";
            try{
                var result = sforce.apex.executeAnonymous("ApexController","timeoutMerge",{});
                console.log('Timeout!!');
            }catch(e) {
                alert(e);
            }
        }
        </script>

VF(because apex:commandButton doesn't work in the facet I used <input type:"button">):
 
<apex:commandButton value="Action" action="{!action}" status="Loading" reRender="pageSecId" timeout="{!intMergeTimeout}"/>                            
                            <apex:actionStatus id="Loading" >
                                <apex:facet name="start">                                    
                                    <div>
                                        <div class="popupBackground" />
                                        <div class="PopupPanel">
                                            <div align="center">
                                                <div class="slds-text-heading--label">
                                                    <b>Merging...</b>                                                    
                                                </div>
                                                <div class="slds-button slds-button--brand">
                                                    <input type="button" onclick="timeout()" value="Cancel"/>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </apex:facet>                                
                                <apex:facet name="stop"></apex:facet>
                            </apex:actionStatus>





 
Best Answer chosen by jkc
Tarun Khatri (TK)Tarun Khatri (TK)
Hello jkc,

Update your javascript code as below:
<script>
	function timeout(){
		callTimeOut();
	}
</script>

Add below code in your VF page:
<apex:actionFunction name="callTimeOut" action="{!timeoutMerge}" id="timeoutFcn" />
On click of your button, Javascript function get call. This JS function calls actionfunction in VF page which in turn calls your method in controller.

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

All Answers

Pramodh KumarPramodh Kumar
you can call in many ways...

Javascript Remoting,

use action function to call the method.


Thanks,
Pramodh
salesforce mesalesforce me
Hi check this once...
 
Try it
<apex:page controller="calljavascript_cls" >
<script>
  function func()
  {
  alert('function calling');
  }
  </script>
  <apex:outputText value="{!callfunc}" escape="false"></apex:outputText>
 
</apex:page>
-------- apex class --------------
public class calljavascript_cls
{
public string callfunc{get;set;}
public calljavascript_cls()
{
    callfunc='<script> func(); </script>';
}
}

 
Tarun Khatri (TK)Tarun Khatri (TK)
Hello jkc,

Update your javascript code as below:
<script>
	function timeout(){
		callTimeOut();
	}
</script>

Add below code in your VF page:
<apex:actionFunction name="callTimeOut" action="{!timeoutMerge}" id="timeoutFcn" />
On click of your button, Javascript function get call. This JS function calls actionfunction in VF page which in turn calls your method in controller.

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 
This was selected as the best answer
jkcjkc
Thanks TK :)