1

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.

Error Details :

System.Net.WebException was unhandled by user code
  HResult=-2146233079
  Message=The remote server returned an error: (400) Bad Request.
  Source=System
  StackTrace:
       at System.Net.HttpWebRequest.GetResponse()
       at WebApplication2.About.CreateSampleOpportunity() in D:\Fenil\DotNetProjects\WebApplication1\WebApplication2\About.aspx.cs:line 381
       at WebApplication2.About.Page_Load(Object sender, EventArgs e) in D:\Fenil\DotNetProjects\WebApplication1\WebApplication2\About.aspx.cs:line 41
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
3
  • 1
    The body of the response will have more details about why the request is invalid. At a guess, your date format is wrong, it should be 2015-03-02
    – superfell
    Commented Mar 2, 2015 at 16:43
  • Thanks for reply sir. I have tried with all date format, With Probability, Without Probability. I think issue is with "StageName" or "Probability". Both are Compalasary Filelds and one is numeric and other one is Picklist(Dropdown). Commented Mar 3, 2015 at 5:10
  • You need to capture and examine the response body, IIRC, its available as a property on the thrown exception.
    – superfell
    Commented Mar 3, 2015 at 15:33

1 Answer 1

0

The problem was in the date format."CloseDate":"3/2/2015"

Right date format : "CloseDate":"2015-03-02"

{ "Name":"FFNEw","CloseDate":"2015-03-02","StageName":"Prospecting","Probability":10 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .