3

I just spun up a new production org and I am getting the following error when trying to send a POST request to my REST endpoint.

Nested object for polymorphic foreign key must have an attributes field before any other fields.

POST Request

{
   "account": {
      "name": "test"
   }
}

Apex Class

@RestResource(urlMapping='/leadImport')
global without sharing class LeadImportRequestHandler {

    global class ResponseMessage {
        public Boolean isSuccess;
        public String message;
    }

    @HttpPost
    global static ResponseMessage doPost() {
        ResponseMessage response = new ResponseMessage();
        RestRequest req = new RestRequest();
        RestResponse restRes = new RestResponse();
        Account account = new Account();

        try {            
            String body = RestContext.request.requestBody.toString();
            account = (Account)JSON.deserialize(body, sObject.class);
            req = RestContext.request;
            restRes = RestContext.response;
            insert account;
        } catch (Exception e) {
            restRes.statusCode = 400;
            response.isSuccess = false;
            response.message = e.getMessage();
        }
        return response;
    }
}

The class throws the error on the following line of code account = (Account)JSON.deserialize(body, sObject.class);

I know sending the below request solves the issue but for other Salesforce orgs I have worked on I simply sent the first request and it worked fine. The difference between the new org is the apex class is not associated with a Sites page where classes on the previous orgs are. Any ideas or suggestions would be appreciated. Thanks!

Working Request

{
    "attributes": {
        "type": "Account"
    },
    "name": "test"
}
  • try to change sObject.class to account.class – Ilya Lepesh Jul 14 '16 at 17:32
  • off topic, but why don't you just do restRes.statusCode = response.getStatusCode(); – EricSSH Jul 14 '16 at 17:59
  • @EricSSH the getStatusCode() method is an HttpResponse class method. So this wouldn't work in this situation since I am using my own wrapper class, ResponseMessage. – Gabe Wonders Jul 18 '16 at 17:01
5

According to the JSON documentation:

Object deserialize(String jsonString, System.Type apexType)

Deserializes the specified JSON string into an Apex object of the specified type.

You can use sObject with attributes, or specify exact class type:

string body = '{"account":{"name":"test"}}';
Account acc = (Account)JSON.deserialize(body, Account.class);
system.debug(acc);

USER_DEBUG [3]|DEBUG|Account:{Name=test}
  • How would one deserialize this in JS instead of Apex code? I'm using JSForce and running into this same error, but I don't have access to the Account.class object – Big Money May 15 at 16:57

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.