I'm trying to execute a class that comes in a managed package (I have a development and a deployment (sandbox) org that I'm testing this).
I have a button in a Custom Object that calls a WebService. It works fine in development. However, in the testing org, where the packages are managed (and have a namespace!) it doesn't work because the class can't be found without the namespace.
I found a way to get the prefix via Apex already. What I'm trying to do is to anonymously execute this code via Ajax, in the custom button that is on the object's page.
This is the code I'm trying to execute:
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
var prefix = sforce.apex.executeAnonymous("
ApexClass myClass = [
SELECT NamespacePrefix, Name
FROM ApexClass
WHERE Name = \'MyClassName\'
];
if (myClass.NamespacePrefix == null)
{
return \'\';
}
else
{
return myClass.NamespacePrefix;
}
");
var result = sforce.apex.execute(
prefix + "MyClassName", // class
"Clonar", // method
{IdIWantToPassToTheClass: '{!TheCustomObject.Id}'}
);
window.location = result;
The part where it tries to execute the Apex code anonymously actually works on a developer's console (tested in both orgs).
This is throwing me an Unexpected token ILLEGAL error in an alert.
Is my code wrong somewhere? What am I missing?
Will I have create a custom setting to keep a string with the namespace? (I'd rather not do this)