2

I am trying to update a contact record using rest api but facing issue like POST method is not allowed.

If I use PUT method. it is also not allowed

I have tried this solution How can I make a PATCH HTTP Callout from Apex? but didn't worked.

Any issue with code. any help

 id conid= ApexPages.currentPage().getParameters().get('conid');
        try{
            Boolean isSandbox = true;
            String uri          = 'https://' + (isSandbox ? 'test' : 'login') + '.salesforce.com/services/oauth2/token';
            String clientId     = EncodingUtil.urlEncode(clientId     ,'UTF-8');
            String clientSecret = EncodingUtil.urlEncode(clientSecret ,'UTF-8');
            String username     = EncodingUtil.urlEncode(USERNAME,'UTF-8');
            String password     = EncodingUtil.urlEncode(PASSWORD,'UTF-8');

            String body =   'grant_type=password&client_id=' + clientId + 
                            '&client_secret=' + clientSecret +
                            '&username=' + username + 
                            '&password=' + password; 

            HttpRequest hRqst   = new HttpRequest();
            hRqst.setEndpoint(uri);                     
            hRqst.setMethod('POST');                
            hRqst.setTimeout(6000); 
            hRqst.setBody(body);
            Http h = new Http();
            HttpResponse hRes = h.send(hRqst);

            Map<String,String> res = (Map<String,String>) JSON.deserialize(hRes.getBody(),Map<String,String>.class);

            hRqst  = new HttpRequest();
            system.debug('===hR11qst==='+res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);
            hRqst.setEndpoint(res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);                     
            // hRqst.setMethod('PUT');
            hRqst.setHeader('X-HTTP-Method-Override','PATCH');
            hRqst.setMethod('POST');
            hRqst.setHeader('Accept', 'application/json');
            hRqst.setHeader('Authorization','Bearer ' + res.get('access_token'));
            objCon.HasOptedOutOfEmail = true;
            String body1 = '{"id": '+conid+', HasOptedOutOfEmail":"true"}';
            hRqst.setBody(JSON.serialize(objCon));
            h = new Http();
            system.debug('===hRqst==='+hRqst);
            hRes = h.send(hRqst);
            system.debug('===body==='+hRes.getBody());
            // objCon.HasOptedOutOfEmail  = true;
            // update objCon;
        }
        catch(Exception ex){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage());
            ApexPages.addMessage(myMsg);
        }

1 Answer 1

5

The correct method is mentioned in the Update a Record documentation. X-Http-Method-Override is not used by Salesforce. Instead, you need to add ?_HttpMethod=PATCH to have the Salesforce REST API interpret your request as a PATCH request.

If you use an HTTP library that doesn't allow overriding or setting an arbitrary HTTP method name, you can send a POST request and provide an override to the HTTP method via the query string parameter _HttpMethod.

hRqst.setEndpoint(
  res.get('instance_url')+
  '/services/data/v40.0/sobjects/Contact/'+conid+'?_HttpMethod=PATCH');                     
hRqst.setMethod('POST');

When you need to use PUT, you can also use this technique as well.

2
  • Awesome thank you so much.
    – Ratan Paul
    Dec 29, 2017 at 4:08
  • How do we handle this via REST Assured Java? Feb 21, 2023 at 9:06

You must log in to answer this question.

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