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

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

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

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.