0

For remoting functions I use a reusable wrapper class to standardize responses and reduce code duplication. When trying to use the same principle for the rest classes, any external response class referencing returns the error "global methods do not support return type of DD_Wrapper.Response". I also run into the same error whenever the response class is local but it's accessing an external class. Is this inherit to REST classes or any way around this? Thanks!

@RestResource(Urlmapping='/v1/endpoint')
global without sharing class My_Rest_Class {

    @HttpPost
    global static DD_Wrapper.Response doPost() {
        return new DD_Wrapper.Response('success',200);
    }
}



public with sharing class DD_Wrapper {

    public class Response{
        public Boolean isSuccess            {get; set;}
        public String message               {get; set;}
        public List<Object> records         {get; set;}
        public Object record                {get; set;}

        //Standard message response
        public Response(Boolean isSuccess, String message){
            this.isSuccess = isSuccess;
            this.message = message;
        }
        .....
3

Global methods can't return public objects (access restriction). You would also need to make the wrapper a global object. This isn't a particular limitation of rest resources, but instead is a limitation of how the access control keywords (private, public, global) operate. For example, you also can't return a private class in a public method.


As specified in the documentation, only the following types are allowed:

  • Apex primitives (excluding sObject and Blob).
  • sObjects
  • Lists or maps of Apex primitives or sObjects (only maps with String keys are supported).
  • User-defined types that contain member variables of the types listed above.

If you were allowed to return an Object, you could violate this rule, so Object cannot appear anywhere in the return value's custom type.

8
  • I had tried that out before, sorry forgot to post. The problem seems to be the reference in the wrapper class. Below will work in the reference but if you uncomment the global List<Object> records it will fail with the error. Global with sharing class Wrapper { Global class Response{ Public Boolean isSuccess; Public String message; //global List<Object> records; public Response(Boolean isSuccess, String message){ this.isSuccess = isSuccess; this.message = message; } } }
    – Kevin B
    Sep 13 '18 at 20:43
  • @KevinB I think you mean sobject instead of object. And everything in the class should be global.
    – sfdcfox
    Sep 13 '18 at 20:47
  • I was using Object to abstract it so I could pass in any object (like what I do for the remoting objects) so the top level is consistent and then the record/records returned are custom response classes or other sobjects. That structure works for remoting objects: @RemoteAction public static Wrapper.Response functionHere() { .... return new DD_Wrapper.Response(true, NULL, <custom object>);
    – Kevin B
    Sep 13 '18 at 21:06
  • @KevinB Edited my answer. You can't use object, because it would allow you to violate the rules of the API outlined in the documentation. You have to use concrete data types.
    – sfdcfox
    Sep 13 '18 at 21:15
  • What's interesting is the code below will work (took the external class out of the equation). The problem is if I change ChildResponse record > Object record it fails even though Object is listed as a primitive type in the docs. @RestResource(Urlmapping='/v1/endpoint') global without sharing class My_Rest_Class { @HttpPost global static Response doPost() { return new Response(); } Global class Response{ Public Boolean isSuccess; Public String message; global ChildResponse record; } Global class ChildResponse {} }
    – Kevin B
    Sep 13 '18 at 21:22

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.