2

I am using CRM Web API to perform CRUD operations.

Field List Image of all custom fields Wherever Disabled is set, it is for field security.

For POST request when I execute this request with below code, it throws Bad Request error. My code sample is as below:

URL https://BaseURL/api/data/v8.2/transactions

jsonData

{
    "transactionnumber":"123456789123",
    "transactionamount":"500",
    "transactiondate":"2018-01-26T03:00:00.000Z"
}

Code:

public Dictionary<string, string> ExecutePostRequest(string entityName, string jsonFormattedData)
{
     string requestURL = GenerateRequestURL(entityName);
     HttpContent content = new StringContent(jsonFormattedData, Encoding.UTF8, "application/json");
     return DoRequest(requestURL.ToString(), content, HttpMethod.Post);
}

DoRequest method //which actually executes http request

private Dictionary<string, string> DoRequest(string requestUrl, HttpContent content, HttpMethod httpMethod)
{
    Dictionary<string, string> returnValue;
    HttpResponseMessage responseMessage;
    returnValue = new Dictionary<string, string>();
    try
    {
        HttpClient httpClient = SetUpCRMConnection();
        HttpRequestMessage request;
        request = new HttpRequestMessage(httpMethod, requestUrl);
        switch (httpMethod.ToString())
        {
            case "PATCH":
            case "POST":
            case "PUT":
                request.Content = content;
                break;
            case "DELETE":
                break;
        }
        responseMessage = httpClient.SendAsync(request).Result;
        return GetFormattedResponse(responseMessage);
    }
    catch (UriFormatException ex)
    {
        logger.Error(ex.InnerException);
        returnValue.Add("ERROR", "Invalid URL generated: " + ex.InnerException.ToString());
        return returnValue;
    }
    catch(Exception ex)
    {
        logger.Error(resourceManager.GetString("CRM_ConnectionLost"),ex);
        returnValue.Add("ERROR", resourceManager.GetString("CRM_ConnectionLost"));
        return returnValue;
    }
}
Ranjana
  • 85
  • 9

1 Answers1

2

Validate your entity name & attribute schema names, looks like they are custom but missing publisher prefix, ex. new_

Also datetime field should have .000 before Z.

"transactiondate":"2018-01-26T03:00:00.000Z"

Update:

String & Datetime datatypes should be enclosed in quotes.

Int, Decimal, Currency (Money) datatypes should not be enclosed in quotes. Try this:

"transactionamount":500
  • Yes, it is customized attribute and there is prefix in original but while posting out it here, I removed that. And for `datetime` partI have performed changes for the same and tested again but still throwing the same error. – Ranjana Jan 25 '18 at 13:39
  • @Ranjana we cannot guess what could be the issue.. try yourself with required attributes, any business rule? – Arun Vinoth-Precog Tech - MVP Jan 25 '18 at 14:29
  • It's a pure custom entity and no business rule is attached with it. There is one primary key "transactionid" which will contain GUID and I am not trying to inserting it. I don't know if there is anything else I am missing here as I am naive to CRM. But for your help I can do 1 more thing. I am adding list of all the fields in question. – Ranjana Jan 25 '18 at 14:41
  • 2
    @Ranjana create/update with minimum fields, remove/add one by one to sort out the data type/typo issues.. this way you can troubleshoot.. – Arun Vinoth-Precog Tech - MVP Jan 25 '18 at 14:43
  • With this it is working for DateTime variable when it come to lookup field it is still throwing Bad Request. My data is as below: `{"new_transactionnumber":"113","new_transactionamount":650,"‌​new_transactiondate"‌​:"2018-01-29T15:01:0‌​0.000Z","new_contact‌​id@odata.bind":"/con‌​tacts(afb9e006-6be5-‌​e711-80ed-bef8067862‌​23)","new_moveid@oda‌​ta.bind":"/new_moves‌​(896f2dcd-b2fa-e711-‌​80ef-bef806786223)"}‌​`. For this I have tried following reference:(https://community.dynamics.com/crm/b/crminogic/archive/2016/04/13/set-values-of-all-data-types-using-web-api-in-dynamics-crm-through-c). – Ranjana Jan 29 '18 at 09:46
  • Here's the question: (https://stackoverflow.com/questions/48502747/bad-request-error-while-adding-a-lookup-guid-field-in-crm-database-using-web-api) – Ranjana Jan 29 '18 at 14:22