Created
October 31, 2017 15:38
-
-
Save iloveitaly/c4e330f9e1a839b266c5b1aaca5a6836 to your computer and use it in GitHub Desktop.
Copy fields from a transaction record to a payment in NetSuite. Built by http://SuiteSync.io/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: <mike@suitesync.io> | |
Description: Copies department, class, and location from the order (SalesOrder, Invoice) | |
to the transaction (customer deposit, customer refund, and customer payment). | |
This can be customized to copy custom | |
Installation: | |
1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl | |
2. User Event | |
3. Name: Copy Order Fields to Transaction | |
4. ID: _suitesync_copy_invoice_fields | |
5. After Submit: afterSubmit | |
6. Deployments: CustomerDeposit on Create, or CustomerPayment on Create | |
*/ | |
// Utils | |
// https://gist.github.com/iloveitaly/db7d532e772b67f5b81d0199d094301f | |
function log(msg) { | |
nlapiLogExecution('DEBUG', msg); | |
} | |
function error(msg) { | |
nlapiLogExecution('ERROR', msg); | |
} | |
function isEmpty(obj) { | |
return obj === undefined || obj === null || obj === ""; | |
} | |
function afterSubmit() { | |
var recordType = nlapiGetRecordType(); | |
var recordId = nlapiGetRecordId(); | |
var locationsEnabled = nlapiGetContext().getFeature('LOCATIONS'); | |
var classesEnabled = nlapiGetContext().getFeature('CLASSES'); | |
var departmentsEnabled = nlapiGetContext().getFeature('DEPARTMENTS'); | |
var currentDepartment = null; | |
var currentClass = null; | |
var currentLocation = null; | |
if(departmentsEnabled) { | |
currentDepartment = nlapiGetFieldValue('department'); | |
} | |
if(classesEnabled) { | |
currentClass = nlapiGetFieldValue('class'); | |
} | |
if(locationsEnabled) { | |
currentLocation = nlapiGetFieldValue('location'); | |
} | |
if(!isEmpty(currentDepartment) && !isEmpty(currentLocation) && !isEmpty(currentClass)) { | |
log("department and location are already set, skipping"); | |
return; | |
} | |
var applyingTransactionId = null; | |
var applyingTransactionType = null; | |
if(recordType == 'customerdeposit') { | |
var salesOrderId = nlapiGetFieldValue('salesorder'); | |
if(isEmpty(salesOrderId)) { | |
log("customer deposit is not linked with sales order"); | |
return | |
} | |
applyingTransactionId = salesOrderId; | |
applyingTransactionType = 'salesorder' | |
} else { | |
if(nlapiGetLineItemCount('apply') > 1) { | |
log("contains more than one applying transaction, skipping"); | |
return; | |
} | |
applyingTransactionId = nlapiGetLineItemValue('apply', 'doc', 1); | |
applyingTransactionType = nlapiGetLineItemValue('apply', 'type', 1); | |
} | |
if(departmentsEnabled && isEmpty(currentDepartment)) { | |
var departmentId = nlapiLookupField(applyingTransactionType, applyingTransactionId, 'department', false); | |
if(!isEmpty(departmentId)) { | |
nlapiSubmitField(recordType, recordId, 'department', departmentId); | |
} | |
} | |
if(classesEnabled && isEmpty(currentClass)) { | |
var classId = nlapiLookupField(applyingTransactionType, applyingTransactionId, 'class', false); | |
if(!isEmpty(classId)) { | |
nlapiSubmitField(recordType, recordId, 'class', classId); | |
} | |
} | |
if(locationsEnabled && isEmpty(currentLocation)) { | |
var locationId = nlapiLookupField(applyingTransactionType, applyingTransactionId, 'location', false); | |
if(!isEmpty(locationId)) { | |
nlapiSubmitField(recordType, recordId, 'location', departmentId); | |
} | |
} | |
// NOTE here you can copy custom information from the invoice to the CustomerPayment | |
} | |
if(nlapiGetContext().getExecutionContext() == 'debugger') { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment