function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wesnoltewesnolte 

JavaScript Remoting calls not working from managed package

Hey,

 

I have an application that works very well in unpackaged, or unmanaged packaged code. However if I upload it to a managed package then the JavaScript remoting calls fail. I know this because nothing happens in my app and the following appears in the console:

 

 

Here I notice that it's not prepending my package namespace, a bug from managed packaging when it originally came out! Having a look at the sourcecode confirms this:

 

 

I've FBI'ed the code out but there's no namespace to the doubleunderscores in this code.

 

This leads me to believe I've uncovered a bug which makes me sad since my app is about 80% JavaScript. Any chance I'm doing something incorrectly and this isn't a bug?

 

Wes

Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

I think I know what you mean, thanks. I've done the below and it's working well:

 

if(typeof MyPackage === 'undefined'){

            window["MyPackage"] = {};

            

           MyPackage.Controller = Controller;

}

 

// All code only refers to MyPackage.Controller

All Answers

wesnoltewesnolte

I think I've confirmed this bug as when I manually add the prefix the code works. However this destroys my development process and organisation as code is developed in separate dev orgs, promoted to an unmanaged package org and finally promoted to the packaging org. Manually adding a prefix in the final stage could be a rather large headache.

 

Wes

cwall_sfdccwall_sfdc

Hey Wes.  Yes, in Winter '12 Visualforce remoting Javascript proxies are fully qualified with the namespace prefix.  This ensures that there are no object naming conflicts when remoted pages/components are mixed with other remoted pages/components.

 

We understand that this breaks the multi-org development process where code is migrated from non-namespaced orgs to a namespaced org where it's packaged.  One workaround is to conditionally create a namespace reference to the generated proxy.  For example:

 

<script>

if (!myNamespace) {  // 'myNamespace' is the packaged org's namespace

    window["myNamespace"] = {};

    myNamespace.myRemotedController = myRemotedController;  // w/in the dev non-namespaced orgs, myRemotedController should be generated earlier

}

</script>

 

Note that the above must go after Visualforce's Javascript proxy generation.

 

We are working on a solution.  Thank you for your patience.

 

 

wesnoltewesnolte

Thanks for the reply. The suggested code doesn't work, it was the first thing I tried too :) It kills my JS code with a 'namespace is undefined' error. I've had to solve it with a try-catch. Yuck!

 

Wes

cwall_sfdccwall_sfdc

Can you posted your solution?

 

It's not clear where 'namespace is undefined' occurred.

wesnoltewesnolte

Hey

 

Sorry I didn't reply earlier, I didn't receive an email notification. The error happens on the first line of your suggested call:

 

if (!myNamespace)

 

 

Wes

cwall_sfdccwall_sfdc

Does checking for undefined work (see below)?

 

Just to be clear, in the non-namespace dev orgs, Javascript remoting calls should be written WITH the namespace of the deployment org.  The recommended Javascript check noted above is applied / will be executed only in the non-namespace requests.

 

 

if (typeof PackageOrgnNamespace !== 'undefined') {

    // manually create namespaced obj

}

 

// example remoting call

PackageOrgnNamespace.MyController.invokeRemoteMethod(...);

wesnoltewesnolte

I think I know what you mean, thanks. I've done the below and it's working well:

 

if(typeof MyPackage === 'undefined'){

            window["MyPackage"] = {};

            

           MyPackage.Controller = Controller;

}

 

// All code only refers to MyPackage.Controller

This was selected as the best answer
cwall_sfdccwall_sfdc

Sweet.

 

Favor:  Can you mark this thread as having a solution and point to solution post?  Others have or may have the same issue and I'd like to spread the word on this workaround.  Thanks. 

wesnoltewesnolte

Yeah I'll write something up, wordpress has excellent SEO. Thanks for your help.

 

Wes

tgagne56tgagne56

Is there a way to use Javascript Remoting on a class that IS NOT the page's controller?

 

I was disappointed I had to add a controller="MyRemoteReadyClassName" in the <apex:page> tag to call a globally static function.  Inside my VisualForce page attempts to use MyRemoteReadyClassName.RemoteFunction failed beause MyRemoteReadyClassName was undefined.

 

Our org doesn't have a namespace, so I'm wondering, how would I handle instances where the methods were in multiple classes?