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
Leonardo WuLeonardo Wu 

How should I execute the method in the custom object and execute the js code at the sametime

User-added imageThis is my custom object method,I use it to update my list; 
I also write a js code under the button.  But when I done the js code, the page can jump,but my custom metod is not execute.
I want ask how should I can execute the custom object method,and also execute the js method.
User-added imageUser-added image  Thin
Best Answer chosen by Leonardo Wu
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Hi Wu Leonardo,

You can execute the javscript method(change();) ,after execution of Action method(getSave()).

Below is the code to execute it in the above specified order. Change onClick to onComplete event in the VF Page.
 
<apex:commandButton action="{!getSave}" value="" onComplete="change();" />

Once you click on the custom button, the action method will start execute. If the action method is succesfully executed , then the javascript action OnComplete will start execute.
If there are any validation errors while executing the action method, then the javascript action onComplete won't execute.

As you said above in onClick event, Both the methods will initaite at same time.

Hope this will help .



Thanks
Satya.
 

All Answers

Shashikant SharmaShashikant Sharma
Just make these changes to your visualforce page code.

onclick="change();"
replace it with
onclick="return change();"

In the change() fucntion body
remove these statements
window.opener.location.href
window.close();


and add this statement
return false;

You could navigate with apex class method by making return type as PageReference instead of void.


Thanks
Shashikant









 
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Hi Wu Leonardo,

You can execute the javscript method(change();) ,after execution of Action method(getSave()).

Below is the code to execute it in the above specified order. Change onClick to onComplete event in the VF Page.
 
<apex:commandButton action="{!getSave}" value="" onComplete="change();" />

Once you click on the custom button, the action method will start execute. If the action method is succesfully executed , then the javascript action OnComplete will start execute.
If there are any validation errors while executing the action method, then the javascript action onComplete won't execute.

As you said above in onClick event, Both the methods will initaite at same time.

Hope this will help .



Thanks
Satya.
 
This was selected as the best answer
Leonardo WuLeonardo Wu
Hi  Satya
Thanks for your help. And it's  really working.