代码之家  ›  专栏  ›  技术社区  ›  AjFmO

尝试通过带有JSON主体的POST方法保存客户记录及其地址信息?

  •  0
  • AjFmO  · 技术社区  · 6 年前

    正如标题所示,我试图在NetSuite Record中保存一个客户,但无法通过。

    交易是我需要用it地址来保存客户,但是看起来地址并不像普通值那样传递,而是一个数组。

    这是尸体:

    {
       "recordtype":"customer",
       "entityid":"John Doe",
       "companyname":"ABC Inc",
       "subsidiary":"1",
       "email":"jdoe@email.com",
       "custentity_cseg_region":"3",
       "addressbook":[
          {
             "addressbookaddress":{
                "zip":"104-8315",
                "country":{
                   "internalid":"JP",
                   "name":"Japan"
                },
                "addressee":"ABC Inc",
                "city":"Tokyo",
                "addr1":"1-1, 1-Chome",
                "attention":"John Doe",
                "override":false
             },
             "addressbookaddress_text":"Lorem ipsum dolor sit amet\nno putant tamquam his\nclita option utroque id quo, ne noster epicurei sed",
             "defaultbilling":true,
             "defaultshipping":true,
             "isresidential":false,
             "label":"1-1, 1-Chome"
          }
       ]
    }
    

    记录确实保存了,但它不接受地址。


    编辑:

    这就是SuiteScript的样子。


    function getRecord(datain) {
        return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
    }
    
    function createRecord(datain) {
        var err = new Object();
        if (!datain.recordtype) {
            err.status = 'failed';
            err.message = 'missing recordtype';
            return err;
        }
    
        var record = nlapiCreateRecord(datain.recordtype);
    
        for (var fieldname in datain) {
            if (datain.hasOwnProperty(fieldname)) {
                if (fieldname != 'recordtype' && fieldname != 'id') {
                    var value = datain[fieldname];
                    if (value && typeof value != 'object') {
                        record.setFieldValue(fieldname, value);
                    }
                }
            }
        }
    
        nlapiLogExecution('DEBUG', 'zip = ' + datain.zip);
        record.selectNewLineItem('addressbook');
    
        record.setCurrentLineItemValue('addressbook', 'attention', datain.attention);
        record.setCurrentLineItemValue('addressbook', 'addressee', datain.companyname);
        record.setCurrentLineItemValue('addressbook', 'addr1', datain.addr1);
        record.setCurrentLineItemValue('addressbook', 'addr2', datain.addr2);
        record.setCurrentLineItemValue('addressbook', 'addr3', datain.addr3);
        record.setCurrentLineItemValue('addressbook', 'city', datain.city);
        record.setCurrentLineItemValue('addressbook', 'state', datain.state);
        record.setCurrentLineItemValue('addressbook', 'zip', datain.zip);
        record.setCurrentLineItemValue('addressbook', 'country', 'US');
        /*record.setCurrentLineItemValue('addressbook', 'country', datain.country);*/
        record.setCurrentLineItemValue('addressbook', 'label', 'billing address');
        record.commitLineItem('addressbook');
    
        var recordid = nlapiSubmitRecord(record);
        nlapiLogExecution('DEBUG', 'id = ' + recordid);
    
        var nlobj = nlapiLoadRecord(datain.recordtype, recordid);
        return nlobj;
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Avi    6 年前

    根据您的请求JSON对象,您需要将RESTlet代码更新为如下所示

    function createRecord(datain) {
    var err = new Object();
    
    if (!datain.recordtype) {
        err.status = 'failed';
        err.message = 'missing recordtype';
        return err;
    }
    
    var record = nlapiCreateRecord(datain.recordtype);
    
    for (var fieldname in datain) {
        if (datain.hasOwnProperty(fieldname)) {
            if (fieldname != 'recordtype' && fieldname != 'id') {
                var value = datain[fieldname];
                if (value && typeof value != 'object') {
                    record.setFieldValue(fieldname, value);
                }
            }
        }
    }
    
    var addressBookDataList = datain.addressbook;
    
    // since addressbook is an array
    // loop through the array and create/set address records on Customer
    addressBookDataList.forEach(function (data) {
      record.selectNewLineItem('addressbook');
    
      record.setCurrentLineItemValue('addressbook', 'attention', data.attention);
      record.setCurrentLineItemValue('addressbook', 'addressee', data.companyname);
      record.setCurrentLineItemValue('addressbook', 'addr1', data.addr1);
      record.setCurrentLineItemValue('addressbook', 'addr2', data.addr2);
      record.setCurrentLineItemValue('addressbook', 'addr3', data.addr3);
      record.setCurrentLineItemValue('addressbook', 'city', data.city);
      record.setCurrentLineItemValue('addressbook', 'state', data.state);
      record.setCurrentLineItemValue('addressbook', 'zip', data.zip);
      record.setCurrentLineItemValue('addressbook', 'country', 'US');
      /*record.setCurrentLineItemValue('addressbook', 'country', data.country);*/
      record.setCurrentLineItemValue('addressbook', 'label', 'billing address');
      record.commitLineItem('addressbook');
    })
    
    nlapiLogExecution('DEBUG', 'zip = ' + datain.zip);
    
    var recordid = nlapiSubmitRecord(record);
    nlapiLogExecution('DEBUG', 'id = ' + recordid);
    
    var nlobj = nlapiLoadRecord(datain.recordtype, recordid);
    return nlobj;
    

    }