2

I need to remotely call an apex method in a Visualforce controller, but the JavaScript code is not working.

Controller method:

global with sharing class MemberPoliciesController {

    @remoteAction
    global static String resetPassword() {
         return 'Test';
    }
}

JavaScript remoting:

<apex:page standardController="contact" extensions="MemberPoliciesController" showHeader="true" sidebar="true">
    <script>
        function changePassword() {
            sforce.connection.sessionId = "{!$Api.Session_ID}";
            alert("I am here");
            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.MemberPoliciesController.resetPassword}',
                ,
                function(result, event) {
                    if (event.status) {
                        alert(result);
                    } else if (event.type === 'exception') {
                        alert(event.message);
                    } else {
                        alert(event.message);
                    }
                },
                {escape: true}
            );
        }
    </script>
</apex:page>
  • What does the page look like? Is the controller amongst its attributes? – bigassforce Aug 6 '15 at 18:30
2

When you invoke the action, you are including an extra comma that is causing problems.

invokeAction() requires a list of parameters and you can't do this with ,, which is what you have. Since you are not passing a parameter, just take out one of the commas and pass the handler function, like this:

 '{!$RemoteAction.MemberPoliciesController.resetPassword}', 
  function(result, event){
  • This would be the issue, I wasn't sure if the extra ',' was needed for a no parameter method. Thanks! – TemporaryFix Aug 6 '15 at 18:36
  • Cool - I just thought I'd say that I also use the syntax used by @NSjonas. It reads a little more clearly, IMO. If this answer cleared up the issue, you can mark it as accepted. – Caspar Harmer Aug 6 '15 at 18:42
1

I prefer using the following syntax:

className.methodName([parameters (optional) ... ,] function(result, event) {
    if (event.type == 'exception') {
       //handle exception 
    }else{
       //do something with result
    }
});

Keep in mind JS is case sensitive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.