I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

400 - Bad request while inserting record for lookup field

I am using Microsoft Dynamics 365 and to access its database I am using Web API approach.

In here, I am having a field which is of lookup type and is having lookup values like below: Field information

In here, consider its code values as below:

Pending: 101
Booked : 102
...

And what I am passing as JSON data is:

{
    "statuscode":"101"
}

I have also tried like below:

"statuscode":101
"statuscode":"Booked"

But none of them is working for me. Can someone guide me on this?

EDIT 1: PUT Request

[ { "statuscode":101, "statecode":0 }, { "statuscode":101, "statecode":0 } ]

StringBuilder requestURL;
requestURL = new StringBuilder();
requestURL.Append(GenerateRequestURL(entityName));
requestURL.Append("(" + strGuID + ")");
HttpContent content = new StringContent(jsonFormattedData, Encoding.UTF8, "application/json");

Dictionary<string, string> returnValue;
HttpResponseMessage responseMessage;
returnValue = new Dictionary<string, string>();
try
{
    HttpClient httpClient = SetUpCRMConnection();
    HttpRequestMessage request;
    request = new HttpRequestMessage(httpMethod, requestUrl);
    request.Content = content;
    responseMessage = httpClient.SendAsync(request).Result;
    return GetFormattedResponse(responseMessage);
}

Upvotes: 2

Views: 380

Answers (1)

First of all, this is not lookup. This is picklist a.k.a optionset.

Then statecode (status/state) & statuscode (status reason) are conjoined twins. You have to set both at the same time & most important - they should be a valid combination.

For example:

This is for Account to set it inactive.

// State code value
account["statecode"] = 1;
//  status reason Value
account["statuscode"] = 2;

Similarly, you have this combination for your entity, put it together.

Something like this:

entity["statecode"] = 1; //check this for "Active" in Status dropdown
entity["statuscode"] = 101; //for pending

Upvotes: 2

Related Questions