5

I have a webservice, and I'm trying to send a List of Apex classes to my SFDC org. Before I even get anywhere in my POST method Salesforce just returns an error.

Sample JSON:

{"FieldParser": {
{"fieldName": "producerCode", "value": ""}, {"fieldname": "description", "value": "otherval"}}

Code:

@RestResource(urlMapping='/StagingREST/*')
global with sharing class EVantageAccountRest
{
  global List<FieldParser> FieldParser;

  global class FieldParser {
    public String fieldName;
    public String value;
  }

  public static JSON2Apex parse(String json) {
    return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
  }

  @HttpPost
  global static JSONStatus saveAccount(String jsonString)
  {
    //it doesn't even reach the code in here.
    System.debug('string = ' + jsonString);
    return new JSONStatus(false, 'string = ' + jsonString, null);
  }
}

JSON2Apex:

public class JSON2Apex {

    public List<FieldParser> FieldParser;

    public class FieldParser {
        public String fieldName;
        public String value;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

Any ideas? For some reason SFDC seems to think this JSON string being passed is bad.

6
  • You appear to have an extra set of {} in your JSON, shouldn't it be {"FieldParser": { "fieldName": "producerCode", "value": ""}}? Without seeing your JSON2Apex class it's hard to figure out what Apex is expecting from your JSON. Sep 29, 2014 at 15:13
  • @AlexTennant My bad I simplified it too much. I believe I need all of those {} Adjusting JSON.
    – dphil
    Sep 29, 2014 at 15:15
  • Any chance of including the JSON2Apex class too? Sep 29, 2014 at 15:18
  • @AlexTennant I included that class, but currently it's not even being used. It was one of the many things I tried, so maybe I should have removed the reference to it to begin with. It doesn't even get to the point where it tries to parse the JSON string.
    – dphil
    Sep 29, 2014 at 15:42
  • Hmm. That's very odd. How are you calling the REST service? Workbench, Java, something else? Sep 29, 2014 at 15:54

1 Answer 1

7

Since you gave the REST method a String parameter, the platform is expecting input of the form

{ "jsonString" : "some string" }

If you're wanting to pass in a list of FieldParser objects, one way to do this is to have the REST method take a list of FieldParser as an argument, like this:

@HttpPost
global static JSONStatus saveAccount(List<FieldParser> fieldParsers)

In this case, you'll need to pass the JSON in the form

{ 
  "fieldParsers" : [
    {
      "fieldName": "name1", 
      "value": "value1"
    }, 
    {
      "fieldName": "name2", 
      "value": "value2"
    }
  ]
}

An alternative is to have it take no arguments and get the POST body in the from the request, like this:

@HttpPost
global static JSONStatus saveAccount() {
    List<FieldParser> fieldParsers = (List<FieldParser>)JSON.deserialize(
       RestContext.request.requestBody.tostring(), 
       List<FieldParser>.class);

This would allow you to directly pass a list of FieldParser objects:

[
  {
    "fieldName" : "name1".
    "value" : "value1"
  },
  {
    "fieldName" : "name2".
    "value" : "value2"
  }
]
5
  • 1
    The List<FieldParser> parameters didn't work, but using your other way along with the new way to pass the input did.
    – dphil
    Sep 29, 2014 at 18:13
  • Huh - what was the error? It should work; I'd like to figure out why it didn't...
    – metadaddy
    Sep 30, 2014 at 2:12
  • 1
    [ { "message": "JSON request body must be an object at [line:1, column:2]", "errorCode": "JSON_PARSER_ERROR" } ]
    – dphil
    Sep 30, 2014 at 14:01
  • 1
    Actually, I got the other way to work by making the JSON: {"fieldParsers": [{"fieldName": "ProducerCode__c", "value": "001"}, {"fieldName": "FirstName__c", "value": "Paul"}]}
    – dphil
    Sep 30, 2014 at 14:02
  • 1
    Ah - that does make sense! I'll edit my answer now - thanks for the update!
    – metadaddy
    Sep 30, 2014 at 15:39

You must log in to answer this question.

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