3

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)

1 Answer 1

4

Instead of calling Apex Code through executeAnonymous, I'd suggest one of two other suggestions:

(A) Call the API directly. There's no reason why you can't simply:

sforce.connection.query(
   'SELECT NamespacePrefix FROM ApexClass WHERE Name = \'SomeClass\'', 
   { onSuccess: function(data) {
       var prefix = data.records[0].NamespacePrefix? data.records[0].NamespacePrefix[0]+'.': '';
       var result = sforce.apex.execute(
           prefix+'SomeClass', 'Clonar', { data: value });
   });

(B) Using the REST API or Tooling API

You can call the API directly and perform a query or tooling query to find the class and retrieve its class name. Personally, I'd just call /services/data/v33.0/query using the page's session ID, then get the prefix from the query result. This is functionally the same as using sforce.connection.query but without the SOAP overhead.

As a side note, you were missing a ".", which might have been part of the problem; I suspecting simply adding that will satisfy the original problem of determining the namespace.

2
  • Thank you for your solution, I'm definitely going to try it. Also, could you point where I have missed the dot? I can't find it. Jun 11, 2015 at 11:31
  • 1
    prefix + "MyClassName", // class ... The prefix and class name are separated by a period, as in "MyNS.MyClass". You're missing that separator in your code.
    – sfdcfox
    Jun 11, 2015 at 12:49

You must log in to answer this question.

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