You need to sign in to do that
Don't have an account?
VINAYAK
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
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
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>
...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
I wrote this:
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
onclick="checknull('j_id0:frm1:pgbk1:blk1:blkI1:inscription_no');"
Thanks prageeth! Very useful!
Hope it helps!