I have a public class to get the response like this:

public without sharing class PhoneAndAddress {
  public String id;
  public String address;
  public String phoneNumber;

And a global class for the outside can access and get data

global without sharing class PhoneAndAddressService {
 global static PhoneAndAddress getPhoneAndAddressFromAccount(String accountId) {
   Account infoQuery = new Account([SELECT Id, PhoneNumber__c, Address__c FROM Account WHERE Id = :accountId]};
   PhoneAndAddress info = new PhoneAndAddress();
   info.id = infoQuery.Id;
   info.address = infoQuery.PhoneNumber__c;
   info.phoneNumber = infoQuery.Address__c;
   return info;

Is it possible to use public class as a return value of a global method like this? If not, is there anyway to overwrite or reuse this response class? Thank you!

2 Answers 2


No, you cannot use a public return type to a global method. Same goes for inputs. If you cannot alter the payload in any way, the best you can do may be to serialize it so your return type is String.

return JSON.serialize(info);

If you can edit it, the easiest change is to make it global instead of public.


You can reuse the class if the class is a top-level class or can be redeclared as virtual. Here's a really simple copy-paste example:

global class Demo {
    public virtual class ResultPublic {
        public String message;
    global class Result extends ResultPublic {}
    global static Result doSomething() {
        Result r = new Result();
        r.message = 'Hello World';
        return r;
    /* This code won't compile...
    global static ResultPublic doSomethingElse() {
        ResultPublic r = new ResultPublic();
        r.message = 'Hello World';
        return r;

I should mention that this won't work if the public wrapper class is in a different namespace than the one where it is to be extended to. This wasn't clear in the question, so this answer addresses one possible way that it may be possible under certain circumstances.


You must log in to answer this question.

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