0

Has anyone had any luck getting a PATCH or other method to update an object to work in the REST API in C#?

Whenever I try to use patch I get this response StatusCode: 405, ReasonPhrase: 'Method Not Allowed'

I've tried multiple methodologies. The latest is this

        Uri patchUri = new Uri(fullRequest);
        var patchContent = new StringContent(caseJSON, Encoding.UTF8, "application/json");

        var request = new HttpRequestMessage
        {
            RequestUri = patchUri,
            Method = new HttpMethod("PATCH"),
            Content = patchContent
        };
        request.Headers.Add("Authorization", "Bearer " + oauthToken);

        HttpClient postClient = new HttpClient();


        HttpResponseMessage result = await postClient.SendAsync(request).ConfigureAwait(false);

There is an existing idea to add PATCH to the REST API

But I was wondering if someone had a workaround or an alternative.

0

The Idea you linked to is not relevant to your situation. This is specifically about supporting PATCH in Apex Code. Many of the REST API calls in Salesforce do support PATCH. Odds are, you're trying to call a resource that does not support PATCH. Double-check the documentation for the appropriate verb(s) for your request, and double-check your URL. For example, if you're trying to update a case, the correct URL looks like /services/data/v43.0/sobjects/Case/500xxxxxxxxxxxxYYY (where the last part is a valid Case ID), while if you're trying to "insert or update" a case, it would be something like /services/data/v43.0/sobjects/Case/External_Id__c/12345, where 12345 is the external ID, and External_Id__c is the External ID API name.

0

When I tried to use the salesforce method in C#, GET and POST worked fine. But whenever I tried to execute PATCH, I got a "405 Not Allowed" response.

But here's a SalesforceSharp library I found that will allow PATCH operations. Download the SalesforceSharp class library and add it to your project. Update nuget packages for the library. Reference the class library in your project. Add usings for it in your code. From there it's pretty straighforward.

        var updateObj = //new object with the properties of the sObject to update;
        var client = new SalesforceClient();
        //your connected app credentials go into the flow
        var authFlow = new UsernamePasswordAuthenticationFlow(sfdcConsumerKey, sfdcConsumerSecret, sfdcUserName, sfdcPassword);
        //your token url
        authFlow.TokenRequestEndpointUrl = sfdcTokenUrl;

        try
        {
            client.Authenticate(authFlow);
        }
        catch (SalesforceException ex)
        {
            "Authentication failed: " + ex.Error + ex.Message;
        }

        client.Update("Case", case.Id, updateObj);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.