2

I'am trying to call a apex class method from another apex class which is a part of unlocked org dependant package.

Below is the class in package (Class is present as a part of installed package):

global with sharing class AccountUtil {
    global AccountUtil() {

    }
    
    global static List<Account> getFewAccounts() {
        return AccountUtil2.getSomeAccounts();
    }
    
 }

Class which is outside of unlocked package which does not have any namespace :

global class AccountUtil2 {
    
    global AccountUtil2() {
        
    }

    global static List<Account> getSomeAccounts() {
        return [SELECT Name FROM Account LIMIT 3];
    }
    
    global List<Account> getAccounts() {
        return [SELECT Name FROM Account LIMIT 3];
    }
}

When I execute namespace.AccountUtil.getFewAccounts(); in Anonymous window , it shows below error :

Dependent class is invalid and needs recompilation: Class namespace.AccountUtil : Variable does not exist: AccountUtil2

Is there a restriction which says that apex classes outside of unlocked, org dependant classes cannot be referenced inside the package?

Use case - We are in the process of converting large codebase into unlocked, org dependant packages gradually. Hence, this becomes important to create namespaces in modularised way.

5
  • I assume you have given the org dependent unlocked package a namespace? Is there a reason these classes and methods have all been marked global?
    – Phil W
    Commented Apr 26 at 6:59
  • 1
    @PhilW: Yes , namespace is given to unlocked, org dependant package. I had to mark AccountUtil2 global since it was not visible inside the package class. Package class can be public. The package class gets saved with reference to AccountUtil2 but fails at runtime.
    – Rohit
    Commented Apr 26 at 7:02
  • 1
    Use of global is required when working across the namespace boundary, though only in the direction of dependency. If the packaged class must be directly accessible outside the package, it must be global. If the package must directly access a class outside the package, it must be global. I cannot help specifically with the org dependent nature of your package, but I can say that when using unlocked packages, I found providing them a namespace caused me grief, so I ended up not using a namespace.
    – Phil W
    Commented Apr 26 at 7:28
  • 1
    BTW, if I were decomposing a large metadata "happy soup" into packages with namespace, I would ensure all the packages shared the same namespace and I'd use the @NamespaceAccessible annotation to allow communication across their boundaries with each other.
    – Phil W
    Commented Apr 26 at 7:30
  • @PhilW: Thank you for the details. Thats the last option to move all the metadata directly into same namespace but it defeats the purpose of org dependant pacakges. Another approach which I'am thinking of is to create cluster of dependencies and migrate them separately. I did find this link which states the scenarios for dependency.
    – Rohit
    Commented Apr 26 at 7:37

0

You must log in to answer this question.

Browse other questions tagged .