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
VINAYAKVINAYAK 

how can i call a apex method from javascript

Hi

 

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 .

 

Regards

Vinayak sharma

JimRaeJimRae

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

 

http://salesforcesource.blogspot.com/2009/09/edit-and-delete-command-for-your.html

prageethprageeth

Hello VINAYAK;

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">

<script>

function confirmDelete() {

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

return doDelete;

}

</script>

<apex:form>

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

</apex:form>

</apex:page> 

 

ThomasTTThomasTT

...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. 

 

 

ThomasTT

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

I wrote this: 

 

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

 

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????

vanessenvanessen

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 

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

gazazellogazazello

Thanks prageeth! Very useful!

DixitDixit
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}"/>                                                                   
                                                                </apex:commandLink>

Hope it helps!