4

I have a 1GP Managed Packaged with Released Versions (but no customers only Dev orgs) and it contains a global class with various abstract methods that subscribers could override.

global with sharing abstract class MyApi {

    // ABSTRACT
    
    global abstract void anOperation(...);
    global abstract void anotherOperation(...);

    ...
} 

Now I tried adding another global abstract method but it fails to deploy in the Packaging org with:

Cannot add new final methods to virtual or abstract classes

Until now, I didn't know if this seemingly undocumented restriction that you can not extend global classes by adding more restrictions.

Question: What are my options to mitigate that?

  1. Can I leverage @deprecated in any way?
  2. Should I simply kill this class and invent a new one? Including refactoring all my code?
  3. Should I switch to 2GP? Not sure if that is even possible?
  4. Is there other ways I am currently not thinking of?

2 Answers 2

8

You cannot add a new abstract method to a published global class since this would invalidate any existing implementations/extensions of that class; they would be incomplete.

You can, however, add new virtual method(s) and provide stub implementation(s) of the new method(s) so any existing implementation/extension classes are still complete.

If that is enough, go for it.

A word of warning around the suggestion of using @deprecate... don't. See this answer for why.

It is also worth noting that global interfaces impose even more strict requirements; once released these cannot be modified at all. This is, again, to prevent existing implementations from being made incomplete (missing implementations of added methods).

1

I solved the problem very simply by making the new method virtual instead:

global with sharing abstract class MyApi {

    // ABSTRACT
    
    global abstract void anOperation(...);
    global abstract void anotherOperation(...);

    // VIRTUAL 
    // To not break exisiting handlers just virtual (see https://salesforce.stackexchange.com/a/410879/256)
    global virtual Rules aThirdOperation(...) {
        throw new ApplicationException('aThirdOperation() method not implemented by the current implementation.');
    }
   
    ...
}

You must log in to answer this question.

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