I have copied the code below (I have completely split function into two distinct iterations in an attempt to find the problem.
Scenario: Create new contact. When contact id created from blank record - company is entered and script below behaves as desired( select primary address and populates contact record with primary rather than default billing address of customer on contact record. This scenarios works fine.
Scenario: Contact record is created from customer record. Company and address are pre-populated on record load by Netsuite functionality. Desired result is for primary address for company to replace default billing address of company. Script fires as expected and executes as expected all the way until it writes the line item and commits. It does not commit the new line item and record saves with the default billing address for customer. I have tried it as a beforeSubmit - same result. Can't do it as afterSubmit as it tries to override the new record and throws an error. Any help is appreciated.
function clientFieldChanged(type, name, linenum)
{
if(name == 'company')
{
var stCustomerID = nlapiGetFieldValue(name);
nlapiLogExecution('DEBUG','<success> company:'+stCustomerID);
var recCompany = nlapiLoadRecord('customer', stCustomerID);
var count = recCompany.getLineItemCount('addressbook');
for (var i=0; count != null && i < count; i++)
{
var j = i + 1;
var primary = recCompany.getLineItemValue('addressbook','label', j);
nlapiLogExecution('DEBUG','<primary> company:'+primary);
if (primary == "Primary")
{
var newaddress1 = recCompany.getLineItemValue("addressbook", "addr1", j);
var newaddress = recCompany.getLineItemValue('addressbook', 'addrtext', j);
var newaddress2 = recCompany.getLineItemValue("addressbook", "addr2", j);
var newaddress3 = recCompany.getLineItemValue("addressbook", "addr3", j);
var newcity = recCompany.getLineItemValue("addressbook", "city", j);
var newstate = recCompany.getLineItemValue("addressbook", "state", j);
var newzip = recCompany.getLineItemValue("addressbook", "zip", j);
//Add first line to sublist
nlapiSelectLineItem('addressbook', 1);
nlapiSetCurrentLineItemValue('defaultshipping', 'F');
nlapiCommitLineItem('addressbook');
nlapiSelectNewLineItem("addressbook");
nlapiSetCurrentLineItemValue("addressbook", "addr1", newaddress1);
nlapiSetCurrentLineItemValue("addressbook", "addr2", newaddress2);
nlapiSetCurrentLineItemValue("addressbook", "addr3", newaddress3);
nlapiSetCurrentLineItemValue("addressbook", "city", newcity);
nlapiSetCurrentLineItemValue("addressbook","state", newstate);
nlapiSetCurrentLineItemValue("addressbook", "zip", newzip);
nlapiSetCurrentLineItemValue("addressbook", "defaultbilling", "T");
nlapiSetCurrentLineItemValue("addressbook", "defaultshipping", "T");
nlapiCommitLineItem("addressbook");
}
}
} else if (name == 'lastname')
{
//this part is to overwrite for contact created from Customer record
var stCustomerID = nlapiGetFieldValue('company');
nlapiLogExecution('DEBUG','<success> company:'+stCustomerID);
if ((stCustomerID != null) && (stCustomerID !== ""))
{
nlapiLogExecution('DEBUG','<failure> company:'+stCustomerID);
nlapiLogExecution('DEBUG','<success> company:'+stCustomerID);
var recCompany = nlapiLoadRecord('customer', stCustomerID);
var count = recCompany.getLineItemCount('addressbook');
for (var i=0; count != null && i < count; i++)
{
var j = i + 1;
var primary = recCompany.getLineItemValue('addressbook','label', j);
if (primary == "Primary")
{
var newaddress1 = recCompany.getLineItemValue("addressbook", "addr1", j);
var newaddress2 = recCompany.getLineItemValue("addressbook", "addr2", j);
var newaddress3 = recCompany.getLineItemValue("addressbook", "addr3", j);
var newcity = recCompany.getLineItemValue("addressbook", "city", j);
var newstate = recCompany.getLineItemValue("addressbook", "state", j);
var newzip = recCompany.getLineItemValue("addressbook", "zip", j);
nlapiLogExecution('DEBUG','<success> company:'+newaddress1);
// nlapiRemoveLineItem('addressbook', 1);
//Add first line to sublist
nlapiInsertLineItem('addressbook',2);
nlapiSetLineItemValue('addressbook', "addr1", 2, newaddress1);
nlapiSetLineItemValue("addressbook", "addr2", 2, newaddress2);
nlapiSetLineItemValue("addressbook", "addr3", 2, newaddress3);
nlapiSetLineItemValue("addressbook", "city", 2, newcity);
nlapiSetLineItemValue("addressbook","state", 2, newstate);
nlapiSetLineItemValue("addressbook", "zip", 2, newzip);
nlapiSetLineItemValue("addressbook", "defaultbilling", 2, "T");
nlapiSetLineItemValue("addressbook", "defaultshipping", 2, "T");
nlapiCommitLineItem("addressbook");
}
}
}
}