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>
1
  • What does the page look like? Is the controller amongst its attributes? Commented Aug 6, 2015 at 18:30

2 Answers 2

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){
2
  • This would be the issue, I wasn't sure if the extra ',' was needed for a no parameter method. Thanks! Commented Aug 6, 2015 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. Commented Aug 6, 2015 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .