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
kallam salesforce1kallam salesforce1 

How to Insert multiple records by using restAPI POST method?

 @HttpPost
    global static Contact CreateContacts(){
        RestRequest request = RestContext.request;
        String requestBody = request.requestBody.toString();
       
        list<Contact> con = (List<Contact>) JSON.deserialize(requestBody, list<Contact>.class);
        insert  con;
        return con;
    }

Passing Request Body>>

{"records" :[ { "FirstName": "test1", "LastName": "hello", }, { "FirstName": "test2", "LastName": "hello", }, { "FirstName": "test3", "LastName": "hello", }]

this code is not working.
sfdc98sfdc98
Hi try below code,


@HttpPost webService static String doPost() {
Account account = new Account();
RestRequest req = RestContext.request;
List<jsonWrap> jsonWrapList = (List<jsonWrap>)JSON.deserialize(req.requestbody.tostring(),List<jsonWrap>.class);
return 'Account Success';
}
public class jsonWrap{
String Namex;
String phonex;
String websitex;
}
}



Sample Json
[ { "Namex": "test1", "phonex": "12312", "websitex": "test.com" }, { "Namex": "test2", "phonex": "12312", "websitex": "test.com" }, { "Namex": "test2", "phonex": "12312", "websitex": "test.com" } ]

please mark as bestanswer if it helps.