+ Start a Discussion

how can i call a apex method from javascript



I have a method in the controller to delete a record when i click on a apex:outputLink   ,but before i delete a record i have to ask for a confirmation using javascript ,How can i call a apex method from a javascript using the user in put if the user says yes it should execute otherwise it should not execute at all .



Vinayak sharma


Here is a great tutorial on how to do exactly this using an actionfunction.  Thanks to Sam for writing it.





I don't think that you need to call the Controller Method using javascript here. You can use Javascript only to display the confirmation message. According to the users response you can decide whether to invoike or not the "action" of the command link. See following example.

In this example I assume that your controller method which deletes the record is "deleteRecord()".  


<apex:page controller="MyController">


function confirmDelete() {

var doDelete = confirm('Are you sure?');

return doDelete;




<apex:commandlink value="delete" action="{!deleteRecord}" onclick="return confirmDelete();"/>





...You still can use apex markups and you don't need any method in your controller if you just want to delete a record


<apex:commandLink value="Delete" action="{!URLFOR($Action.Contact.Delete, contact.id)}" onclick="if(!confirm('Are you sure?')){return false;}"/>


onclick is fired before action, and if it returns false, the following executions are cancelled.

URLFOR + $Action is the offitial way to fire SFDC standard event. Of course, apex:outputLink also works.


Only if you want to do more than just deleting a record, you need your method in your controller and call it from action... but still you don't need javascript if you use apex:commandLink + onclick + return false as prageeth mentioned. 




Message Edited by ThomasTT on 03-17-2010 11:46 AM
Message Edited by ThomasTT on 03-17-2010 11:47 AM

I wrote this: 


   function checknull(textid) {
   if(document.getElementById(textid).value.length == 0){
   alert('Veillez saisir une valeur'); 
   return false;
   return true;


and my button is : 


<apex:commandButton action="{!updateInscription}" onclick="checknull('j_id0:frm1:pgbk1:blk1:blkI1:inscription_no');" value="Mettre à jour les présences"/>


still,even if it returns false,the page is submitted....why????


Oh..**bleep**...i know what is missing...its the return keyword that has been omitted...:-)

it should have been :

onclick="return checknull('j_id0:frm1:pgbk1:blk1:blkI1:inscription_no');" and not 



Thanks prageeth! Very useful!

Use an actionfunction with the delete method in your controller if you are using an html button or use the "action" in a commandlink or apex button, then add in the command button something as following in the "onclick" event.
<apex:commandLink onclick="if(!window.confirm('Do you really want to delete the item?')) return false;" action="{!deleteitem}" value="Delete" rerender="tableone" >
<apex:param name="deleteparam" value="{!register.id}" id="deleteRegister" assignTo="{!deletethis}"/>                                                                   

Hope it helps!