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
Abhishek Vishwakarma 5Abhishek Vishwakarma 5 

Update multiple records using REST AP

Hi Folks,

I have a scenario where I need to update near by 200 records. I tried using the PATCH (PATCH /vXX.X/composite/sobjects) but it gave me error "Cannot specify in an insert call [id]". I was unable to get the resonse when I was passing JSON value.
My Code::

 Http h1 = new Http();
                HttpRequest req1 = new HttpRequest();
                req1.setHeader('Authorization','Bearer '+accessToken);
                req1.setHeader('Content-Type','application/json');
                req1.setHeader('accept','application/json');
                
                req1.setMethod('POST');
                //req1.setHeader('X-HTTP-Method-Override','PATCH');
                string Reqstr = URLXXXXX+'/services/data/v43.0/composite/sobjects';
                System.debug('----Reqstr--'+Reqstr);
                req1.setEndpoint(Reqstr);

String JsonTree ='{"allOrNone" : false,  "records" : [{ "attributes" : {"type" : "Account"}, "id" : "0012900000Nq1ewAAB", "Phone" :27000}]}';

I also checked with this example : https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_sobjects_collections_update.htm 

Please let me know if someone has faced this issue. Thanks!!
Best Answer chosen by Abhishek Vishwakarma 5
Abhishek Vishwakarma 5Abhishek Vishwakarma 5
Hi Raj, 

Thanks for helping out!! It didn't worked while setting PATCH method in header. 

However, I have figured it out the issue and it worked using patch method in url. 
 
string Reqstr = URLXXXXX+'/services/data/v43.0/composite/sobjects?_HttpMethod=PATCH

Marking this question as resolved. 

All Answers

Raj VakatiRaj Vakati
Change your code as below ... YOUR operation should be PATHC not post

 
My Code::

 Http h1 = new Http();
                HttpRequest req1 = new HttpRequest();
                req1.setHeader('Authorization','Bearer '+accessToken);
                req1.setHeader('Content-Type','application/json');
                req1.setHeader('accept','application/json');
                
                req1.setMethod('PATCH');
                //req1.setHeader('X-HTTP-Method-Override','PATCH');
                string Reqstr = URLXXXXX+'/services/data/v43.0/composite/sobjects';
                System.debug('----Reqstr--'+Reqstr);
                req1.setEndpoint(Reqstr);

String JsonTree ='{"allOrNone" : false,  "records" : [{ "attributes" : {"type" : "Account"}, "id" : "0012900000Nq1ewAAB", "Phone" :27000}]}';

 
Abhishek Vishwakarma 5Abhishek Vishwakarma 5
Hi Raj, 

Thanks for helping out!! It didn't worked while setting PATCH method in header. 

However, I have figured it out the issue and it worked using patch method in url. 
 
string Reqstr = URLXXXXX+'/services/data/v43.0/composite/sobjects?_HttpMethod=PATCH

Marking this question as resolved. 
This was selected as the best answer