1

I have recently developed an Apex endpoint like this:

@RestResource(urlMapping='/ImportContacts/*')
global with sharing class RESTContactImportController {

    @HttpPost
    global static void importFromJSON(List<RESTContactImportDTO> contacts){
        // Do stuff
    }
}

The problem is I think list of user-defined types are not allowed since in the documentation it states:

These return and parameter 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.

So, what would be the best workaround to be able to receive a list of custom data types? The error I am currently receiving is:

[
    {
        "errorCode": "APEX_ERROR",
        "message": "Exception: HttpPost methods do not support parameter type of List<RESTContactImportDTO>\n\n"
    }
]

I figured one option could be to try and catch the body and deserialize it in the Apex class, but I'm not sure if that's the proper approach.

1

I figured one option could be to try and catch the body and deserialize it in the Apex class, but I'm not sure if that's the proper approach.

It's the only approach in Apex. Do something like that:

String json = RestContext.request.requestBody.toString();

And then deserialize to Dto

System.JSON.deserialize(json, Your.class);
1
  • My user-defined type contains "member variables of the types listed above" as per the documentation, but I still get HttpPost methods do not support parameter type of [my type]. This seems to be a half-implemented Apex feature and in most real-life cases, you probably need to deserialize it yourself as this answer explains.
    – Frans
    Jul 26 at 19:09

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.