5

please explain what is the difference between these two methods? I found this in documentation:

  • You can use the $RemoteAction global to automatically resolve the correct namespace, if any, for your remote action.
  • Configuration details for the invocation, if any.

Example:

<apex:page controller="TestCtrl">
    <apex:includeScript value="{!$Resource.staticjs}"/>
    <script type="text/javascript">

    function sayHello3(helloTo) {
        TouchCR.TestCtrl.sayHello1(helloTo, function(result, event){
            if(event.status) {
                document.getElementById("result3").innerHTML = result;
            }
        });
    }

    function sayHello4(helloTo) {        
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.TestCtrl.sayHello2}',
            helloTo,
            function(result, event){
                if (event.status) {
                    document.getElementById("result4").innerHTML = result;
                }
            }, 
            {escape: true}
        );
    }
    </script>

    <button onclick="sayHello1('John');">direct js@staticres</button><br/>
    <div id="result1">[Results]</div>
    <button onclick="sayHello2('Mike');">remotemanager js@staticres</button><br/>
    <div id="result2">[Results]</div>
    <button onclick="sayHello3('Jane');">direct js@onpage</button><br/>
    <div id="result3">[Results]</div>
    <button onclick="sayHello4('Jane');">remotemanager js@onpage</button><br/>
    <div id="result4">[Results]</div>
</apex:page>

UPDATE:

  • $RemoteAction can not be used from a Static Resource.

in updated example

  • tested on packaging org (namespace prefix enabled)
  • methods sayHello1 and sayHello2 placed in Static Resource.

method sayHello2 which is uses $RemoteAction and placed in Static Resource fails.

console.log

1 Answer 1

6

The difference is that in the first example the class name was added directly:

TestCtrl.sayHello...

And in the second example the $RemoteAction expression was used.

Why? Suppose you have an org with a namespace (package org or whatever). In that case in order to invoke a remote method you will have to add the namespace prefix to the class name: PREFIX.TestCtrl.sayHello(helloTo, ... The disadvantage of this method is that it will not work on the other org with a different namespace prefix. Using $RemoteAction the prefix will be resolved and added automatically!

Use $RemoteAction in an expression to automatically resolve the namespace

4
  • 3
    You mean RemoteAction right not RemoveAction?
    – javanoob
    Commented Aug 24, 2016 at 13:14
  • @javanoob LOL yeah :) Commented Aug 25, 2016 at 7:38
  • @SergeyUtko Any difference in these two methods when using with third party JS libraries, like AngularJs etc? Commented Aug 25, 2016 at 9:56
  • 1
    @DenisPereverzev There is no difference. Both functions are the same except for for namespace prefix. Commented Aug 25, 2016 at 11:47

You must log in to answer this question.

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