3

Solved?

The root cause is my inaccuracy in object initiating: i just forgot to add new.

But the misleading error message is still in place. So other APEX devs may hit the same issue.

Initial Question

It's a bit strange thing i hit, but since i am not very experienced APEX dev yet... Please give me an idea what's wrong below?

I am coding a custom code which is going to force sync our customer data from SFDC to Zendesk. So i installed a managed package which should do all the Zendesk API work. The package's namespace prefix is zdapiv1.

This code which uses the prefix compiles like a charm:

@isTest

global with sharing class ZdSyncOrg {
    private static zdapiv1.ZendeskConnection zconn;

    private static zdapiv1.ZendeskConnection getConnection() {
        if (null == zconn) {
            zconn = zdapiv1.ZendeskConnection.createWithAPIToken('subdomain','email@example.com','token');
        }
        return zconn;
    }

    @isTest static void testGetConnection() {
        zdapiv1.ZendeskConnection conn = getConnection();
        System.assertNotEquals(null, conn);
        System.assertEquals(conn, zconn);
    }
}

But when i add another method i see [OPERATION FAILED]: classes/ZdSyncOrg.cls: Variable does not exist: zdapiv1 (Line: 23, Column: 22).

Here is the example:

@isTest

global with sharing class ZdSyncOrg {
    private static zdapiv1.ZendeskConnection zconn;
    private static zdapiv1.ZendeskOrganizationsAPI orgApi;

    private static zdapiv1.ZendeskConnection getConnection() {
        if (null == zconn) {
            zconn = zdapiv1.ZendeskConnection.createWithAPIToken('subdomain','email@example.com','token');
        }
        return zconn;
    }

    @isTest static void testGetConnection() {
        zdapiv1.ZendeskConnection conn = getConnection();
        System.assertNotEquals(null, conn);
        System.assertEquals(conn, zconn);
    }

    private static zdapiv1.ZendeskOrganizationsAPI getOrgApi() {
        if (null == orgApi) {
            // The error points to the following line:
            orgApi = zdapiv1.ZendeskOrganizationsAPI(getConnection());
        }
        return orgApi;
    }
}

That puzzles me a lot. Why the same namespace prefix cannot be used in the other method? And how to work that around?

4

Looking at the Github code "ZendeskOrganizationsAPI" does not have static methods .You will have to use an Instance of the class and use constructors designed in the class

private static zdapiv1.ZendeskOrganizationsAPI getOrgApi() {
    if (null == orgApi) {
        // The corrected line
        zdapiv1.ZendeskOrganizationsAPI orgApi = new zdapiv1.ZendeskOrganizationsAPI(getConnection());
    }
    return orgApi;
}
  • 2
    It doesn't matter if it had static methods, because OP is trying to call the class itself as a static method. That said, your correction should work just fine. – sfdcfox Oct 2 '16 at 5:59
  • Exactly, the problem has not so much to do with static methods, but more with the lack of the new keyword. – rael_kid Oct 2 '16 at 9:41
  • that makes sense, thank you! the problem seems in the confusing error message which is totally misleading. – hlopetz Oct 2 '16 at 9:59
2

You'll want to read Type Resolution and System Namespace for Types for more details, but the short version is that Salesforce goes through a few possible combinations of places to look for a type, and returns that rather cryptic error message if the algorithm fails to find a suitable data type.

Since you were trying to call a reference to a method, it first looked for a local variable, then a static variable, then your local classes. Even though the namespace was correct, custom namespaces don't contain methods, so it gave up.

By reading the document I linked here, and understanding how type resolution works, you'll be better prepared to fix this error in the future.

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.