2

So I am new to js remoting and callback functions. I was trying to implement js remoting example from salesforce doc which worked. However on trying to tweak the callback, I am unable to get any result.

function getRemoteAccount() {
    var accountName = document.getElementById('acctSearch').value;

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.AccountRemoter.getAccount}',
        accountName, 
        handlebaar, 
        {escape: true}
    );
    var handlebaar = function(result, event){
            if (event.status) {
            alert(result.Id);
                // Get DOM IDs for HTML and Visualforce elements like this
                document.getElementById('remoteAcctId').innerHTML = result.Id
                document.getElementById(
                    "{!$Component.block.blockSection.secondItem.acctNumEmployees}"
                    ).innerHTML = result.Name;

            } else if (event.type === 'exception') {
                document.getElementById("responseErrors").innerHTML = 
                    event.message + "<br/>\n<pre>" + event.where + "</pre>";
            } else {
                document.getElementById("responseErrors").innerHTML = event.message;
            }
        }
}

Can not the callback method be declared out of the invokeAction.

2
  • What is the event.status you are getting?
    – Saroj Bera
    Jan 4, 2017 at 12:37
  • in doing console debug,it shows : Visualforce Remoting: Parameter length does not match remote action parameters: expected 1 parameters
    – starhunter
    Jan 4, 2017 at 12:48

1 Answer 1

4

I would say that your callback function you are passing is in this case undefined. The error you notice also happens when you are passing an undefined value as one of the parameters.

For example your remote action expects two parameters and one of them is passed as undefined. This will result in the same error.

The reason you get it in this specific case is because you declare your callback function after you invoke the action. In this sense the invokeAction would read accountName, undefined. It sees your callback as a parameter, not a callback function, hence the error.

Simple fix should be to declare your callback before invoking:

function getRemoteAccount() {
    var accountName = document.getElementById('acctSearch').value;

var handlebaar = function(result, event){
        if (event.status) {
        alert(result.Id);
            // Get DOM IDs for HTML and Visualforce elements like this
            document.getElementById('remoteAcctId').innerHTML = result.Id
            document.getElementById(
                "{!$Component.block.blockSection.secondItem.acctNumEmployees}"
                ).innerHTML = result.Name;

        } else if (event.type === 'exception') {
            document.getElementById("responseErrors").innerHTML = 
                event.message + "<br/>\n<pre>" + event.where + "</pre>";
        } else {
            document.getElementById("responseErrors").innerHTML = event.message;
        }
    }

Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.AccountRemoter.getAccount}',
    accountName, 
    handlebaar, 
    {escape: true}
);
}
4
  • Javascript hoisting (adripofjavascript.com/blog/drips/variable-and-function-hoisting) means that this should not be a problem? handlebaar will be pulled up and defined before the remoting invokeAction references it.. order of var/function definitions should be irrelevant? (I'm no javascript expert mind) Jan 4, 2017 at 13:50
  • 1
    It might indeed if it was declared as function handlebaar()... in the case of var handlebaar = function() it doesn't apply. It's mentioned in the examples you provided. Jan 4, 2017 at 14:00
  • ah, I see.. "function expressions" are handled differently (ie not hoisted) - that post was clearly not engaging enough to get me to read right down to the bottom! I hope this fixes the OPs problem then! (@starhunter ?) Jan 4, 2017 at 14:04
  • Yeah that (moving function above invoke)worked.
    – starhunter
    Jan 4, 2017 at 17:29

You must log in to answer this question.

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