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/1e57979edf7a75784a076716bb64db32 to your computer and use it in GitHub Desktop.
Save iloveitaly/1e57979edf7a75784a076716bb64db32 to your computer and use it in GitHub Desktop.
/*
Author: <mike@suitesync.io>
Description: Modifies data on an "inferred" NetSuite invoice created from a standalone Stripe charge
Installation:
1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl
2. User Event
3. Name: SuiteSync Inferred Invoice Customization
4. ID: _suitesync_inferred_invoice
5. After Submit: afterSubmit
6. Deployments: Invoice. Event Type: Create
*/
// Utils
// https://gist.github.com/iloveitaly/db7d532e772b67f5b81d0199d094301f
function isUserInterfaceContext() {
var context = nlapiGetContext();
var executionContext = context.getExecutionContext();
return executionContext == 'userinterface';
}
// we are in the dark ages folks: no native startsWith
function startsWith(str, searchString) {
return str.indexOf(searchString) === 0;
}
function isZero(obj) {
return parseFloat(obj) == 0.0
}
function debugObject(obj) {
for (var i in obj.getAllFields()) {
nlapiLogExecution('DEBUG', i)
}
}
// debugSearchResult(results)
function debugSearchResult(searchResults) {
var searchColumns = searchResults[0].getAllColumns()
var output = ""
for (var i in searchColumns) {
output += debugSearchColumn(searchColumns[i])
}
return output
}
// debugSearchColumn(results[0].getAllColumns()[1])
function debugSearchColumn(searchResultColumn) {
var output = ""
output += searchResultColumn.getLabel() + ", "
output += searchResultColumn.getName() + ", "
output += searchResultColumn.getSummary() + ", "
output += searchResultColumn.getJoin() + ", "
log(output)
// return & log for easy debugging via the "Evaluate Expressions" panel
return output
}
function log(msg) {
nlapiLogExecution('DEBUG', msg);
}
function error(msg) {
nlapiLogExecution('ERROR', msg);
}
function isEmpty(obj) {
return obj === undefined || obj === null || obj === "";
}
// End Utils
// NOTE this needs to be changed to match your account configuration!
var SUITESYNC_INFERRED_INVOICE_ITEM = 1311509;
function afterSubmit() {
var recordType = nlapiGetRecordType();
var recordId = nlapiGetRecordId();
processAfterSubmit(recordType, recordId);
}
function processAfterSubmit(recordType, recordId) {
if(recordType == 'invoice') {
// NOTE the entire record must be loaded to inspect the
var record = nlapiLoadRecord(recordType, recordId);
// NOTE "inferred" invoices created from a Stripe charge always have a single consistent line item
var lineItemCount = record.getLineItemCount("item");
if(lineItemCount == 1) {
// NOTE lines are not zero-indexed
var lineItemInternalId = record.getLineItemValue('item', 'item', 1);
if(parseInt(lineItemInternalId) == SUITESYNC_INFERRED_INVOICE_ITEM) {
modifyInferredInvoice(record);
}
} else {
log("more than a single line item")
}
} else {
log("non-invoice record type, not processing")
}
}
// NOTE designed for use with inferred invoices from standalone Stripe charges featu
// https://dashboard.suitesync.io/docs/invoices#invoices-for-standalone-charges
function modifyInferredInvoice(record) {
// NOTE the Stripe charge description is pulled into the memo
// systems creating charge in Stripe normally create a structured description
// for instance, tito creates a description with the following format:
// EVENT_NAME tickets for REGISTRANT_NAME <REGISTRANT_EMAIL> (Order Reference ORDER_REFERENCE)
// you can use the structure of the memo to change the item
var invoiceMemo = record.getFieldValue('memo');
if(isEmpty(invoiceMemo)) {
log("memo is empty, skipping");
return
}
if(startsWith(invoiceMemo, 'Stripe Description: Awesome Event')) {
// update class, department, location, etc of the record
// record.setFieldValue('class', 1);
// record.setFieldValue('department', 1);
// record.setFieldValue('location', 1);
// update the line item on the record
// NOTE when the `item` field of a lineItem is updated the amount is zero'd output
// store the original amount and set the value of the line item
var originalLineItemAmount = record.getLineItemValue('item', 'amount', 1);
record.setLineItemValue('item', 'item', 1, 384208); // internalId of the NS item
record.setLineItemValue('item', 'amount', 1, originalLineItemAmount);
nlapiSubmitRecord(record, true);
} else {
log("unhandled memo detected, skipping")
}
}
if(nlapiGetContext().getExecutionContext() == 'debugger') {
processAfterSubmit('invoice', 5127407)
}
'debug';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment