3

I learnt that deleting a global method is not allowed and even deprecating it leads to a lot of problems. But can I modify it in any way ? E.g. Suppose I have a global method and I have a requirement to slightly change its signature to include one more parameter -

global virtual override void printFullName (String firstName, String lastName) { 
      System.debug(firstName + lastName);
}

Q1 - Can I change its signature to include one more parameter like ?

global virtual override void printFullName (String firstName, String middleName, String lastName) { 
      System.debug(firstName + middleName + lastName);
}

Q2 - If that's not allowed, can I add another overloaded method and modify the body of the first method to call the overloaded method ?

global virtual override void printFullName (String firstName, String lastName) { 
      printFullName2(firstName, "", lastName);
      
}

global virtual override void printFullName2 (String firstName, String middleName, String lastName) { 
                System.debug(firstName + middleName + lastName);
}

1 Answer 1

7

The body of any packaged global method can be modified just so long as you haven't also marked the method as @deprecated. Take a look at my answer over here on that topic.

Obviously, you cannot change the method's signature since that would be the equivalent of deleting the original version of the method from packaging's perspective.

Adding new, overloaded global methods is allowed.

I would, however, encourage you to think long and hard before making anything global. Once it's there, it's basically impossible to remove again (yeah, there are some ways but these only work in very specific scenarios that are quickly unavailable after customers start installing versions of the package with the global in it).

By the way, there is a design pattern that could help you here, in future. You could use something like the Fluent Interface to construct a parameter object, like:

global class Parameters {
    global Parameters withAbc(Integer abc) {
        this.abc = abc;

        return this;
    }

    global Parameters withXyz(String xyz) {
        this.xyz = xyz;

        return this;
    }
}

Then have the global method accept an instance of these parameters:

global void exampleMethod(Parameters parameters) {
    ...
}

Having used the fluent interface pattern, constructing and passing the parameters can be done in a relatively compact way, like:

theInstance.exampleMethod(new Parameters().withXyz('Hello').withAbc(42));

NB: I didn't show how xyz and abc were stored in the parameters class; these might only be public properties if the external code doesn't need to get hold of these values once set in the parameters instance.

What's great here is that you simply add a new global "withSomething" method to Parameters if you need to extend your API later.

Another option is to use the Builder Pattern, which can pre-validate the parameters to be passed to ensure that mandatory values are defined, but that has more plumbing to set up compared with the fluent interface approach.

3
  • Fantastic explaination. Thank you ! :) Commented May 28, 2021 at 18:51
  • One follow up question please. Suppose if I have the first method declared in an abstract class then would it be a good design pratice to include the overloaded method too in the abstract class or would it be better to just overload in the implementation class ? Commented May 28, 2021 at 19:04
  • That depends on why you have the abstract (base class) and implementation class separation. If it is so you can change behaviours (polymorphism) then I would assume the overloaded method would want to be in the abstract class too.
    – Phil W
    Commented May 28, 2021 at 19:15

You must log in to answer this question.

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