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
h0llyh0lly 

rest api - Add Order to Existing Contrract

Build the json
string insertPacket = "";
insertPacket += "{";
insertPacket += "\"contract\": [";
...etc

string serviceURL3 = "https://mydomain.salesforce.com/services/data/v39.0/commerce/sale/800b000000EJ8SCAA1";//the contract id
StringContent insertstring = new StringContent(insertPacket, Encoding.UTF8, "application/json");

//tried Head, Post, Put. No option for Patch in HttpMethod
HttpRequestMessage request3 = new HttpRequestMessage(HttpMethod.Post, serviceURL3);
request3.Headers.Add("Authorization", "OAuth " + token);
request3.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

//try to override the post(?)
request3.Headers.Add("X-HTTP-Method-Override", "PATCH");
request3.Content = insertstring;
HttpResponseMessage response3 = await queryClient3.SendAsync(request3).ConfigureAwait(continueOnCapturedContext: false);

Returns Error Message:
[{"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'POST' not allowed. Allowed are HEAD,GET,PATCH"}]

 
John TowersJohn Towers
You're not actually using the patch method, you're still POSTing.

You do this: 
HttpRequestMessage request3 = new HttpRequestMessage(HttpMethod.Post, serviceURL3);

That sets the method to POST. The 'X-HTTP-Method-Override' is just a header. It's a common pattern but not one the REST API recognizes. Instead, you can add '_HttpMethod=PATCH' to your endpoint as a URL parameter:
 
string serviceURL3 = "https://mydomain.salesforce.com/services/data/v39.0/commerce/sale/800b000000EJ8SCAA1?_HttpMethod=PATCH";


 
h0llyh0lly
Hi John,
yes I discovered this earlier and got it working. I couldn't find anywhere that this was documented but came across it in a forum post elsewhere.


For anyone else who suffered the same frustrations, here is the code:

public async Task<int> addMyOrderToExistingContract(){

string serviceURL3 = "https://mydomain.my.salesforce.com/services/data/v39.0/commerce/sale/" + contractid + "?_HttpMethod=PATCH";
Salesforce.Force.ForceClient myforceclient1 = new ForceClient(serviceURL3, accesstoken, "v39.0");
HttpClient queryClient3 = new HttpClient();
string insertPacket = "";
insertPacket += "{";
insertPacket += "\"contract\": [";
..build your json

StringContent insertstring = new StringContent(insertPacket, Encoding.UTF8, "application/json");
HttpContent content = new StringContent(insertPacket, Encoding.UTF8, "application/json");
HttpRequestMessage request3 = new HttpRequestMessage(HttpMethod.Post, serviceURL3);
request3.Headers.Add("Authorization", "OAuth " + accesstoken);
request3.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request3.Headers.Add("X-HTTP-Method-Override", "PATCH"); //PROB DONT NEED THIS
request3.Content = insertstring;
HttpResponseMessage response3 = await queryClient3.SendAsync(request3).ConfigureAwait(continueOnCapturedContext: false);
string result33 = response3.Content.ReadAsStringAsync().Result;

return result33.Length;

}

//call the above
int testint = addMyOrderToExistingContract().Result;

 
John TowersJohn Towers
Oh, good.

It's referenced in the REST API docs but it's a bit hidden: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_update_fields.htm

The very last paragraph on the page describes it.

For your example code: you're right that you don't need this line:
 
request3.Headers.Add("X-HTTP-Method-Override", "PATCH"); //PROB DONT NEED THIS

since Salesforce doesn't do anything with it.