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
Fenil Suthar 007Fenil Suthar 007 

Create Opportunity Using rest API; Error : 400 bad request in response

Example for creating Opportunity :
curl https://na1.salesforce.com/services/data/v20.0/sobjects/Opportunity/ -H "Authorization: Bearer token" -H "Content-Type: application/json" -d @newrecord.json -X PATCH
Example request body newrecord.json file :
{ "Name":"FFNEw","CloseDate":"3/2/2015","StageName":"Prospecting","Probability":10 }
My ASP.net code :
using (WebClient client = new WebClient())
{
client.Headers.Add("Authorization", "Bearer " + token.access_token); client.Headers.Add("Content-Type", "application/json");
var request = (HttpWebRequest)(HttpWebRequest.Create(token.instance_url + "/services/data/v20.0/sobjects/Opportunity/"));
request.Method = "POST";
using (var requestWriter = new StreamWriter(request.GetRequestStream()))
{
  requestWriter.Write(json);
  requestWriter.Flush();
  requestWriter.Close();
}
var response = request.GetResponse();
}
Getting Error 400 Bad Request in "request.GetResponse()".
I am able to create Account using same code. I don't Know what wrongs with Opportunity.
Best Answer chosen by Fenil Suthar 007
BalajiRanganathanBalajiRanganathan
You have problem with your date format."CloseDate":"3/2/2015"

use "CloseDate":"2015-03-02"

 

All Answers

David ZhuDavid Zhu
what if you use the latest SFDC version v32.0 instead of V20.0?
BalajiRanganathanBalajiRanganathan
You have problem with your date format."CloseDate":"3/2/2015"

use "CloseDate":"2015-03-02"

 
This was selected as the best answer
BalajiRanganathanBalajiRanganathan
debug and see if you are getting updated JSON Request.
(400) Bad Request is mostly due to incorrect JSON.

You can use workbench to send the JSON Request and see what you are getting.
https://workbench.developerforce.com/login.php

I am sure it is about the date format and you should have date as "2015-03-02"
 
Fenil Suthar 007Fenil Suthar 007
How to create bulk record using rest API?
I am able to create single record using above code. But can't create multiple records.

My request string is :
[{"Name":"Opp1","CloseDate":"2015-05-02","StageName":"Stage1","Probability":"10","Amount":"1500"},{"Name":"Opp2","CloseDate":"2015-02-03","StageName":"Stage2","Probability":"5","Amount":"2000"}]
Error in .net Code : 400 bad request
Error in Workbench : errorCode: METHOD_NOT_ALLOWED
message: HTTP Method 'POST' not allowed. Allowed are HEAD,GET
 
BalajiRanganathanBalajiRanganathan
I don't think inserting multiple records is supported at this time using REST API.
Ramesh Naidu PRamesh Naidu P
add below code to the Curl Call 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,           "GET");
Bhuvanesh MohankumarBhuvanesh Mohankumar
Rest API will support object, 400 error is due to data mismatch.