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 

Update 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/0AASSFGSEID -H "Authorization: Bearer token" -H "Content-Type: application/json" -d @newrecord.json -X PATCH
Example request body newrecord.json file :
{ "Name":"FFNEw","CloseDate":"2015/02/04","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/0AASSFGSEID"));
request.Method = "PATCH";
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()".

 
Anupam RastogiAnupam Rastogi
Hi Fenil,

To start with, I have done similar scenario. However I had Salesforce applications as both the calling and receiving ends. And I observed that though Salesforce API supports PATCH method but when we use it programatically it errors. The error is "Invalid HTTP method: PATCH".

So my first question is, did you check logs for your calling application? Are you getting any errors while calling Salesforce using the PATCH method.

Anyways, you can try using the below modified end point. It should work for you as it did for me.

"/services/data/v20.0/sobjects/Opportunity/0AASSFGSEID?_HttpMethod=PATCH"

Also remember to change the calling method to post. So your code should look like the below - 
 
var request = (HttpWebRequest)(HttpWebRequest.Create(token.instance_url + "/services/data/v20.0/sobjects/Opportunity/0AASSFGSEID?_HttpMethod=PATCH"));
request.Method = "POST";

Thanks
AR

If the reply is helpful and solves your problem then please mark it as best.




 
king s 8king s 8
Hi Anupam Rastogi,

your ans is So help full for me thank you so much...........

thanks
kullayappa
Navya VeeraswamyNavya Veeraswamy

Error: [
    {
        "errorCode": "NOT_FOUND",
        "message": "The requested resource does not exist"
    }
]
Curl:
curl --location --request POST 'https://xyzsolutions7-dev-ed.develop.my.salesforce.com/services/data/v50.0/sobjects/Opportunity/' \
--header 'Authorization: Bearer 00D5h000008Nj5y!ARkAQDD7JVaKTCHHT.dHTnOIUN680XIfXgzvYr3HqVGz7yu5OZWATT9HqljfV5cHHi355tbTNuOrmomzo3Tm1dUY2S_CGAi.' \
--header 'Content-Type: application/json' \
--header 'Cookie: BrowserId=U-v_xWGkEe64ePPDdIi5iA; CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1' \
--data-raw '{
    "Name": "Sample Opportunity",
    "AccountId": "0015h00001UePQgAAN",
    "StageName": "Prospecting",
    "Amount": 10000,
    "CloseDate": "2023-12-06"
}'
 
While trying to create an opportunity with the client credentials access token am getting the above error , in case of authorization code access token i can able to create an opportunity.