PavanKumar GVVS
PavanKumar GVVS

Reputation: 1045

Microsoft.OData.ODataException: Invalid JSON Whle Creating Incident in CRM

While creating Incident(case) in CRM, I am getting Invalid JSON. Unable to create Case An error occurred while validating input parameters: Microsoft.OData.ODataException: Invalid JSON. More than one value was found at the root of the JSON content.

What am i missing?

JSON

    {
"title": "SampleCase1CreatedByConsole",
"description": "This case is created by sample console app",
"[email protected]": "/accounts(000000000-0000-000-00000-0000000)"
 }

API URL:

https://xyzcrm.crm11.dynamics.com/api/data/v9.1/incidents

Headers:

Authorization:Bearer 
OData-MaxVersion:4.0
OData-Version:4.0
Accept:application/json

Code:

          public static async Task<string> CreateCase(string caseTitle, string customerId, string 
      caseDescription = "")
    {
        string jResu = "";
        string createcaseURL = "https://hack90182.crm11.dynamics.com/api/data/v9.1/incidents";
        try
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            string accessToken = "<ACCESSTOKEN>";

            //Body
            string jsonBody = "";
            string accountdatabind = string.Format("/accounts({0})", customerId);
            if (String.IsNullOrEmpty(caseDescription))
            {
                jsonBody = "{'title':'" + caseTitle + 
           "','[email protected]':'/accounts(00000-000-000-000000)'}}";
                //jsonBody = "{'title':'" + caseTitle +  "','customerid_account#odata.bind':'https://hack90182.crm11.dynamics.com/api/data/v9.1/accounts(00000-000-000-000000)'}}";
            }
            else
            {
                jsonBody = "{'title':'" + caseTitle + "','[email protected]':'" + 
          accountdatabind + "','description':'" + caseDescription + "'}}";
            }
            var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
            var client = new HttpClient();
            var message = new HttpRequestMessage(HttpMethod.Post, createcaseURL);
            message.Content = content;
            message.Headers.Add("OData-MaxVersion", "4.0");
            message.Headers.Add("OData-Version", "4.0");
            message.Headers.Add("Accept", "application/json");
            message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var result = client.SendAsync(message).Result;
            if (result != null)
            {
                var createcaseJSON = await result.Content.ReadAsStringAsync();
                jResu = createcaseJSON;//success
            }
            return jResu;

        }
        catch (Exception ex)
        {

            throw;
        }
    }

Upvotes: 0

Views: 1608

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 792

The JSON format requires double quotes instead of single quotes, so you'll have to format your JSON in the same way. For example,

     jsonBody = "{\"title\":\"" + caseTitle + 
           "\",\"[email protected]\":\"/accounts(00000-000-000-000000)\"}";

I replaced ' with \" in the code above.

Upvotes: 1

Related Questions