function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc learner 122sfdc learner 122 

Simple Fix needed in Restapi testclass

Hi Team , i need help in fixing this test class

Wrapper Class:

public class AccountWrapper {
    public List<accRecords> AccountList;
  public  class accRecords {
    public String AccId;  
    public String NextVal;  
  }
  public static AccountWrapper parse(String json){
    return (AccountWrapper) System.JSON.deserialize(json, AccountWrapper.class);
  }

}

My Apex Snippet:

 Map<Id,String> accMap = new Map<Id,String>();
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String bodyJson = req.requestBody.toString();
AccountWrapper reqJSON = AccountWrapper.parse(bodyJSON);
for(AccountWrapper.accRecords acc : reqJSON.AccountList){
accMap.put(acc.AccId,acc.NextVal);
}

My test class is failing above highlighted line,i want to pass list of Account id in JsonBody 

currently i am facing below error message
System.JSONException: Malformed JSON: Expected '{' at the beginning of object  - i think this is i am passing single record
 
Julien SalensonJulien Salenson
Hi sfdc learner 122 or whoever you are :)

In your Apex snippet, you are expecting the JSON to contain an array of Account Records, but it seems you might be passing a single Account Record instead of an array. To fix this, you should ensure that your JSON input matches the expected format.
 
{
    "AccountList": [
        {
            "AccId": "001XXXXXXXXXXXXXXX",
            "NextVal": "SomeValue1"
        },
        {
            "AccId": "002XXXXXXXXXXXXXXX",
            "NextVal": "SomeValue2"
        }
        // Add more records if needed
    ]
}

Each Account Record should be enclosed in curly braces {}.

Please mark this comment as best answer if it's help you.