Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/f7dfb1a2240fccfb15860ff0b621f03d to your computer and use it in GitHub Desktop.
Save iloveitaly/f7dfb1a2240fccfb15860ff0b621f03d to your computer and use it in GitHub Desktop.
// Mike Bianco: <mike@suitesync.io>
// Name: _set_default_warehouse.js
// Description: Sets a "warehouse" (custom field) on invoices and customers if one is not provided
// Custom transaction forms:
// https://debugger.sandbox.netsuite.com/app/common/custom/custforms.nl?whence=
// Standard invoice form:
// https://system.sandbox.netsuite.com/app/common/custom/custform.nl?id=125&nl=F&ft=TRANSACTION&tt=CustInvc&ol=F&e=T
// Standard customer form:
// https://system.sandbox.netsuite.com/app/common/custom/custentryform.nl?id=8&nl=F&ft=ENTITY&sbtp=Customer&rt=&e=T
// Customer custom field:
// https://system.sandbox.netsuite.com/app/common/custom/entitycustfield.nl?id=44&e=T
// Custom field for Stripe link
// https://debugger.sandbox.netsuite.com/app/common/custom/bodycustfield.nl?id=5082&e=T
function debugObject(obj) {
for(var i in obj.getAllFields()) {
nlapiLogExecution('DEBUG', i)
}
}
// debug_search_result(results)
function debug_search_result(search_results) {
var search_columns = results[0].getAllColumns()
var output = ""
for(var i in search_columns) {
output += debug_column(search_columns[i])
}
return output
}
// debug_column(results[0].getAllColumns()[1])
function debug_column(search_result_column) {
var output = ""
output += search_result_column.getLabel()
output += search_result_column.getName()
output += search_result_column.getSummary()
output += search_result_column.getJoin()
nlapiLogExecution('DEBUG', output)
// return & log for easy debugging via the "Evaluate Expressions" panel
return output
}
function isEmpty(obj) {
return obj === undefined || obj === null || obj === ""
}
function log(msg) {
nlapiLogExecution('DEBUG', msg)
}
// ^ generic debugging functions
function testScript() {
// poor mans test suite
var newCustomer = nlapiCreateRecord('customer');
setWarehouseForCustomer(newCustomer)
if(newCustomer.getFieldValue('custentity_warehouse') == null) {
nlapiLogExecution('ERROR', 'customer script broken!')
}
var existingCustomer = nlapiLoadRecord('customer', 8580);
setWarehouseForCustomer(existingCustomer)
var newInvoice = nlapiCreateRecord('invoice');
setWarehouseForInvoice(newInvoice)
if(newInvoice.getFieldValue('location') == null) {
nlapiLogExecution('ERROR', 'invoice script broken!')
}
var existingInvoice = nlapiLoadRecord('invoice', 1988053);
setWarehouseForInvoice(existingInvoice)
// TODO test if warehouse was changed!
}
var COUNTRY_TO_WAREHOUSE_MAPPING = {
"US":"123"
};
var INVOICE_WAREHOUSE_FIELD_ID = 'location';
var CUSTOMER_WAREHOUSE_FIELD_ID = 'custentity_warehouse';
function recordUpdated(type) {
nlapiLogExecution('DEBUG', 'Processing update of type: ' + type)
// reject inline edits
if(type != 'xedit') {
if(type == 'create' || type == 'edit') {
setWarehouseForRecord(nlapiGetNewRecord());
}
}
}
function setWarehouseForRecord(record) {
var recordType = nlapiGetRecordType();
log('Record type: ' + recordType)
if(recordType == 'customer') {
setWarehouseForCustomer(record)
} else if(recordType == 'invoice') {
setWarehouseForInvoice(record)
} else {
nlapiLogExecution('DEBUG', 'Warehouse Mapping', "Uncaught record type: " + recordtype)
}
return record;
}
function setWarehouseForCustomer(customer) {
var warehouse = customer.getFieldValue(CUSTOMER_WAREHOUSE_FIELD_ID);
if(isEmpty(warehouse)) {
var countryIso = getCountryCodeFromCustomer(customer);
var locationInternalId = warehouseIdForCountryIso(countryIso);
customer.setFieldValue(CUSTOMER_WAREHOUSE_FIELD_ID, locationInternalId);
} else {
log("warehouse value for customer is not null")
}
return customer;
}
function setWarehouseForInvoice(invoice) {
var warehouse = invoice.getFieldValue(INVOICE_WAREHOUSE_FIELD_ID);
if(isEmpty(warehouse)) {
var customer = getCustomerFromInvoice(invoice);
var countryIso = null;
if(!isEmpty(customer)) {
countryIso = getCountryCodeFromCustomer(customer);
}
var locationInternalId = warehouseIdForCountryIso(countryIso);
invoice.setFieldValue(INVOICE_WAREHOUSE_FIELD_ID, locationInternalId);
} else {
log("warehouse value for invoice is not null")
}
return invoice;
}
function getCustomerFromInvoice(invoice) {
var customerId = invoice.getFieldValue('entity');
// customerId can be null if not specified on record
if(isEmpty(customerId)) {
return null;
}
return nlapiLoadRecord('customer', customerId);
}
function getCountryCodeFromCustomer(customer) {
var billCountry = customer.getFieldValue('billcountry');
if(!isEmpty(billCountry)) {
return billCountry;
}
var shipCountry = customer.getFieldValue('shipcountry');
if(!isEmpty(shipCountry)) {
return shipCountry;
}
return null;
}
function warehouseIdForCountryIso(countryIso) {
var warehouseInternalId = COUNTRY_TO_WAREHOUSE_MAPPING[countryIso];
if(isEmpty(warehouseInternalId)) {
return '50';
} else {
return warehouseInternalId;
}
}
// NOTE: do not remove! This enables us to run the script using the debugger.
'debug';
if(nlapiGetContext().getExecutionContext() == 'debugger') {
testScript();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment