AjFmO
AjFmO

Reputation: 425

Trying to save a Customer Record with its address information via POST method with JSON body?

As the title suggests, I'm trying to save a Customer in NetSuite Record but can't make it through.

The deal is that I need to save the Customer with it Address, but it seems that the address does not pass like plain value for saying so, instead is an array.

This is the body:

{
   "recordtype":"customer",
   "entityid":"John Doe",
   "companyname":"ABC Inc",
   "subsidiary":"1",
   "email":"[email protected]",
   "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"
      }
   ]
}

The records do save but it doesn't take the address.


Edit:

This how the SuiteScript looks.


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

Upvotes: 0

Views: 1077

Answers (1)

Avi
Avi

Reputation: 2069

As per your request JSON object, you need to update your RESTlet code to something like below

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;

}

Upvotes: 0

Related Questions