-2

below is my code Created a wrapper class like below

public class ResponseWrapper{
    public list<Account> lstaccounts;
    public boolean isError ;
    public string errorCode;
    public integer statusCode;
       public ResponseWrapper(){
         lstaccounts = new list<Account>();
         isError= false;
    }

}

and returning the wrapper class in the Httpget method

@RestResource(urlMapping='/accounts/*')
global with sharing class AccountRestService {
@HttpGet
global static ResponseWrapper getAccounts() {
    ResponseWrapper resWrap = new ResponseWrapper();
    List<Account> accounts;

    try {
        accounts = [SELECT Id, Name FROM Account];
        res.lstaccounts = accounts;
    } catch (Exception e) {
        RestResponse res = RestContext.response;
        resWrap.isError = true;
        res.responseBody = Blob.valueOf(e.getMessage()) = resWrap.errorCode;
        res.statusCode = 500 = resWrap.statusCode;
    }

    return resWrap;
  }
}

but its throwing the Error message Error: Compile Error: global methods do not support return type of ResponseWrapper at line 4 column 31

2

Global methods must return global data types. You need to declare your class as global in order to use it as a return value.

As a side note, there's no point to your try-catch block here, because there's no exception you can possibly catch.

Also, you can chain assignments, but the value you're assigning must be on the far right:

    res.statusCode = resWrap.statusCode = 500;

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.