7

I am giving this below JSON array to a REST API-POST Method : So I can insert Account and Contact, but keep getting an error on JSON deserialization of apex parameter. I don't know how shall i go about it.. Any suggestion would be helpful?

also if i can get some link of reference material to better understand how APex Class consumes JSON data. Thanks!!

{
    "rTest": [{
        "sName": "REST Account",
        "sRating": "Hot",
        "sContact": "Rest Contact"
    }, {
        "sName": "REST Account",
        "sRating": "Hot",
        "sContact": "Rest Contact"
    }]
}
6
  • can you post you apex code ( http post method)
    – Rao
    Jul 30 '14 at 19:28
  • @rao : I am new to this, so not doing much in APex, just written code to see the data : global static string PutAccConMultipl(String rTest) { system.debug(rTest); // to see the data that it gets from the request system.debug(JSON.deserializeUntyped(rTest)); // to see the deserialized data passed in here return 'done'; }
    – user10727
    Jul 30 '14 at 19:35
  • I even tried with this : @HttpPost global static String createMerchandise(String merch) { List<Account> tt = (List<Account>)System.JSON.deserialize(merch, List<@HttpPost global static String createMerchandise(String merch) { List<Merchandise__c> tt = (List<Merchandise__c>)System.JSON.deserialize(rTest, List<Merchandise__c>.class); system.debug(tt); return 'done' }>.class); system.debug(tt); return 'done' }
    – user10727
    Jul 30 '14 at 19:39
  • What exactly is the deserialization error? Jul 30 '14 at 20:00
  • 5
    json2apex.herokuapp.com is your friend. Automatically makes the correct classes given a certain JSON.
    – sfdcfox
    Jul 30 '14 at 20:10
7

The { starts an object and the [ starts an array. It is convenient to deserialise into inner classes (because the class field names must match the JSON names) and then transfer from those into the SObjects:

public class Item {
    public String sName;
    public String sRating;
    public String sContact;
}

@HttpPost
global static String createMerchandise(Item[] rTest) {
    Account[] accounts = new Account[] {};
    for (Item i : rTest) {
        accounts.add(new Account(Name = i.sName, ??? = i.sRating));
    }
    // Other logic relating to Contact?
}

The parameters of the method must match the root JSON object field names and types.

The platform does the deserialisation from JSON to Apex objects automatically if you use the above pattern.

update: fixed a typo in Account creation code

2
  • But got a query, I could see unlike single record processing, when we process JSON array, we have declared a class Item with all the variables we are using in JSON.. I dint get, Why there is a need to do this Class item declaration only for multiple records and not in single record processing...Pl explain?
    – user10727
    Jul 30 '14 at 22:21
  • @user10727 For a single record the method signature could be createMerchandise(String sName, String sRating, String sContact) but without the extra class all you could do would be createMerchandise(String[] sName, String[] sRating, String[] sContact) which is very different JSON. The class provides a group of fields and then you can have an array of those groups of felds.
    – Keith C
    Jul 30 '14 at 22:38
0

How to write test class for global static String createMerchandise(Item[] rTest) ? I have this type of error : Method does not exist or incorrect signature: Class.createMerchandise(List

2
  • Post your code also
    – Reshma
    Dec 5 '16 at 10:31
  • Hi Chris, welcome to SFSE. Please take a moment to visit the help center and read How to Ask and How to Answer. If you have a question to ask, please post a question instead of an answer. You can link to this question if it helps provide context.
    – Adrian Larson
    Dec 5 '16 at 10:56

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.