Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Suggested answer

Status 400 error for creating record in Crm from my web application

Posted on by 35

I am getting status 400 error while creating record in Crm its post method, for get method its working fine, in crm restbuilder its working fine to create record.

string resource = "https://*****.crm4.dynamics.com/";
UserCredential clientredentials = new UserCredential("****t@******onmicrosoft.com", "****@716");
string client_id = "2b572be0-*****-4581-8481-*******";
AuthenticationContext authenticationContext = new AuthenticationContext("">login.microsoftonline.com/.../authorize");
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, client_id, clientredentials);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
httpClient.BaseAddress = new Uri("">orgf65ea8ad.api.crm4.dynamics.com/.../");
account_creation_1 ac_1 = new account_creation_1()
{
name = account_Creation.name,
preetham_reasonforregistration = account_Creation.reason_for_registration,
preetham_DepartmentId = Guid.Parse(account_Creation.Department),
preetham_doctorId = Guid.Parse(account_Creation.Doctor)
};


HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("https://*****.api.crm4.dynamics.com/api/data/v9.2/accounts"))
{
Content = new StringContent(ac_1.ToString(), Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result;
if (response.IsSuccessStatusCode)
{
Console.Write("Account Created");
}
}

I am getting the access token its everything working fine, anybody have any idea where i am wrong

  • Daniel Schneider Profile Picture
    Daniel Schneider 168 on at
    RE: Status 400 error for creating record in Crm from my web application

    Hi,

    httpClient.BaseAddress = new Uri("">orgf65ea8ad.api.crm4.dynamics.com/.../");

    right string of "">?

  • PabloCRP Profile Picture
    PabloCRP 1,086 on at
    RE: Status 400 error for creating record in Crm from my web application

    Hi, please share what the error says, 

    The Uri for the request should only be the entityNameSet ("accounts","leads",etc..) because your are setting a "BaseAddress" property for you HttpClient Class.

    Also I'd use NewtonJson for converting the account_creation_1 to JSON format, doing this "Content = new StringContent(ac_1.ToString(), Encoding.UTF8, "application/json");" doesn't pass a valid JSON payload.

    for testing purposes in connection to microsoft dataverse take a look of the samples in their Official repo at PowerApps-Samples

    I made SolutionContainer a solution repo base on PowerApps-Samples, where your just clone and play change just this fields by your own.

    private static readonly string Url = "https://contoso.crm.dynamics.com";
    private static readonly string ClientId = "00000000-0000-0000-0000-000000000001";
    private static readonly string UserName = "USER@contoso.onmicrosoft.com";
    private static readonly string Password = "YOUR_PASSWORD";

    here Oficial documentation Docs

    regards.

  • Eduteam srl Profile Picture
    Eduteam srl 35 on at
    RE: Status 400 error for creating record in Crm from my web application

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("accounts")) //<--when i changed this got error of invalid url

    preetham_DepartmentId = Guid.Parse(account_Creation.Department),//<-- if this is lookup the fortmat should be "fieldname@odata.bind = entityname_in_plural(GUID)"

    preetham_doctorId = Guid.Parse(account_Creation.Doctor)//<--yes this is lookup

    but now i had removed, now i am just testing with one field "name" but still got the same error

     account_creation_1 ac_1 = new account_creation_1()

                   {

                       name = account_Creation.name

                   };

                   HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("orgf65ea8ad.api.crm4.dynamics.com/.../accounts"))

                   {

                       Content = new StringContent(ac_1.ToString(), Encoding.UTF8, "application/json")

                   };

                   var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result;

    still the same error

  • Suggested answer
    PabloCRP Profile Picture
    PabloCRP 1,086 on at
    RE: Status 400 error for creating record in Crm from my web application

    Hi Eduteam,

    I'd change this lines

    Payload

    account_creation_1 ac_1 = new account_creation_1()
    {
    name = account_Creation.name,
    preetham_reasonforregistration = account_Creation.reason_for_registration,
    preetham_DepartmentId = Guid.Parse(account_Creation.Department),//<-- if this is lookup the fortmat should be "fieldname@odata.bind = entityname_in_plural(GUID)"
    preetham_doctorId = Guid.Parse(account_Creation.Doctor)//<-- if this is lookup the fortmat should be "fieldname@odata.bind = entityname_in_plural(GUID)"
    };

    URI

    //wrong
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("https://*****.api.crm4.dynamics.com/api/data/v9.2/accounts"))
    //good
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("accounts"))

    Check for lookup data WebAPi

    regards

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,768 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,985 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans