0

I have a quirky issue with NetSuite. On a Sales Order which has 'Enable Line Item Shipping' checked, meaning Multiple Shipping Routes are Enabled, the shipping address goes on the item line level.

via SuiteScript, I can access the address on the line level IF it is selected from the address book.

However, when that address is a custom address that is entered on the fly, I have no idea how to get to those fields in a beforeSubmit function.

Any pointers would be much appreciated!

dah97765
  • 679
  • 1
  • 13
  • 29

2 Answers2

0

You can get the selected address details using either or below two

nlapiGetLineItemValue('item','shipaddress',1)
nlapiGetLineItemText('item','shipaddress',1)

The above would only give you the id, or label of address record. But, would be hard to access the address details on the client side.

However, using sub record APIs you can access the record on server-side in user event script using sub record APIs:

var record = nlapiLoadRecord('customer', nlapiGetFieldValue('entity'),{recordmode: 'dynamic'});

//loop through all the addressbooks
if(record.getLineItemValue('addressbook', 'internalid', i) === nlapiGetLineItemValue('item','shipaddress', 1))
record.selectLineItem('addressbook', 2);

//change the sub record value
var subrecord = record.editCurrentLineItemSubrecord('addressbook', 'addressbookaddress');
subrecord.setFieldValue('attention', 'Accounts Payable');
subrecord.commit();
record.commitLineItem('addressbook');

var x = nlapiSubmitRecord(record);
prasun
  • 7,073
  • 9
  • 41
  • 59
  • wouldn't the second set of code you have only work for addresses which are attached to the customer record? My issue lies specifically with custom addresses and trying to get those fields (specifically, the state). I don't think custom addresses are added to the customer record like normal addresses are. bear in mind, I am completely new to NetSuite so maybe I am just not seeing how the second set of code relates to custom addresses. – dah97765 Dec 11 '15 at 22:01
  • I suggest you to create a new sales order line level address from UI. AFAIK it appears as subrecord on customer. – prasun Dec 12 '15 at 02:55
  • prasun - I saw your posts about this in other places so you may be interested. See my accepted answer for the specifics about how this appears to work. – dah97765 Dec 22 '15 at 18:24
0

Figured it out! For those lost souls who venture here in the future:

Below is a function you can use on a sales order line level to loop through the custom addresses and pull out specific information.

Hopefully, this will be added to the NetSuite documentation at some point.

Note that I was doing this specifically for the state information. If you want to see what other fields are available, add &xml=T to the end of a submitted sales order URL and search for iladdrbook in the resuling xml structure. It is actually treated like a line item.

function getCustomAddressFromLineItem(soLineNum) {

    nlapiLogExecution('DEBUG', 'Custom Address', 'Custom Address: Line # ' + soLineNum);

    var addressid = nlapiGetLineItemValue('item','shipaddress',soLineNum); // get the id of the custom address
    var customAddressesLineCount = nlapiGetLineItemCount('iladdrbook'); // get custom address book count

    nlapiLogExecution('debug', 'test', 'addressid: ' + addressid + ' -- linecount: ' + customAddressesLineCount);

    for (var i = 1; i <=customAddressesLineCount; i++)
    {
     var addressinternalid = nlapiGetLineItemValue('iladdrbook','iladdrinternalid',i); // get internal id of custom address book
     if (addressinternalid == addressid) // match it with the id of custom address being set
     {
      var addr = nlapiGetLineItemValue('iladdrbook','iladdrshipaddr1',i);
      var customState = nlapiGetLineItemValue('iladdrbook','iladdrshipstate',i); // get your state
      nlapiLogExecution('debug', 'test', 'address: ' + addr + ' -- state: ' + customState);
      return customState;
     }
    }
}
dah97765
  • 679
  • 1
  • 13
  • 29
  • I like your idea, but undocumented fields might be risky, as NS may change them, aren't you able to get the details from customer for custom addresses? – prasun Dec 22 '15 at 18:52
  • Unfortunately no. Those custom addresses are not stored in the regular address book on the customer record. If you add addresses using the "- Add -" option, then I think you would be correct. If you add addresses using the "- Custom -" option, then they are stored in this weird iladdrbook. – dah97765 Dec 22 '15 at 20:13
  • This was at least confirmed by NetSuite support (not that this means it won't change), but its not 100% unsupported at the very least. – dah97765 Dec 22 '15 at 20:14