如何使用JSON正文在REST API POST方法中传递多个记录

问题描述 投票:-1回答:1

我有一个要求,我需要使用REST API POST方法在我的自定义对象中创建多个记录。现在的问题是我能够一次创建一条记录,而且我无法在一个REST API调用中创建多条记录。我通过传递JSON请求体在网上找到了我将能够创建多个记录。我对集成完全不熟悉,也不了解如何在一个REST API调用中创建mutilple记录,以及如何在REST API中传递JSON请求体。

请有人帮我实现你的要求。我在这里发布我的代码供参考:

@HttpPost
    global static ID createAddress(String Address, String City, String FirstName, String LastName, String Phone, String Email
                                       ) {
        //First find the contact id matching the email.
        String ContactId = [SELECT Id
                              FROM Contact
                              WHERE Email = :Email].Id;
        //Second post the new ListofAddresses to the owner of the email.                                 
        Address__c thisAddress = new Address__c(
            Contact__c=ContactId,
            Address__c=Address,
            City__c=City,
            First_Name__c=FirstName,
            Last_Name__c=LastName,
            Phone__c=Phone,

        ); 
              /* List<Address__c> result = [SELECT Address__c, City__c, First_Name__c, Last_Name__c, Phone__c
                                   FROM Address__c
                                WHERE Contact__c = :ContactId];                          
           if(result.size() > 0){
            return null;
             }else{*/
          insert thisAddress;
          return thisAddress.Id;

             }
json rest salesforce integration apex
1个回答
0
投票

尝试使用此代码以使用Json格式传递多个记录

@RestResource(urlMapping ='/ Account / *')全局类MyRestResource {

    @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;
    }
}

示例Json

[“”Testx“,”phonex“:”12312“,”websitex“:” “test.com”},{“Namex”:“test2”,“phonex”:“12312”,“websitex”:“test.com”}]

© www.soinside.com 2019 - 2024. All rights reserved.