Created
June 19, 2018 12:52
-
-
Save iloveitaly/350363f07b13d7ac81bf81784c1417c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: <[email protected]> | |
Description: Modifies data on an "inferred" NetSuite invoice created from a standalone Stripe charge | |
Link: https://gist.github.com/iloveitaly/350363f07b13d7ac81bf81784c1417c9 | |
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 | |
7. Edit script and modify: | |
* SUITESYNC_INFERRED_INVOICE_ITEM. Set to the internal ID of the NetSuite item | |
* Mapping logic. You'll want to change the item ID, and possibly other values, based on the information coming from Stripe. | |
*/ | |
// 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 | |
var SUITESYNC_INFERRED_INVOICE_ITEM = 4176; | |
function afterSubmit() { | |
var recordType = nlapiGetRecordType(); | |
var recordId = nlapiGetRecordId(); | |
processAfterSubmit(recordType, recordId); | |
} | |
function processAfterSubmit(recordType, recordId) { | |
log("processing invoice" + 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 feature | |
// 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 | |
} | |
var newItemID = null; | |
// NOTE customize matching logic here based on field mapping and the values you'd like to change on the NetSuite side | |
if(startsWith(invoiceMemo, "Stripe Description: WorkLife")) { | |
newItemID = 3957; | |
} else { | |
error("unhandled memo detected, skipping"); | |
return; | |
} | |
// 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 classID = item | |
var originalLineItemAmount = record.getLineItemValue('item', 'amount', 1); | |
record.setLineItemValue('item', 'item', 1, newItemID); | |
record.setLineItemValue('item', 'amount', 1, originalLineItemAmount); | |
record.setLineItemValue('item', 'rate', 1, null); | |
nlapiSubmitRecord(record, true); | |
} | |
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