Created
January 1, 2018 01:30
-
-
Save iloveitaly/a716825a25415c21919b77327e8b491b to your computer and use it in GitHub Desktop.
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: Modifies data on a credit memo if the credit memo represents a dispute | |
Link: TODO | |
Installation: | |
1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl | |
2. User Event | |
3. Name: Modify Credit Memo for Dispute | |
4. ID: _modify_dispute_credit_memo | |
5. After Submit: afterSubmit | |
6. Deployments: Credit Memo. Event Type: Create. Ensure accessible to all roles and executes as admin. | |
7. Update `SUITESYNC_DISPUTE_ITEM_ITEM` to match the ID of the item in your NetSuite account | |
*/ | |
// Utils | |
// https://gist.github.com/iloveitaly/db7d532e772b67f5b81d0199d094301f | |
function isUserInterfaceContext() { | |
var context = nlapiGetContext(); | |
var executionContext = context.getExecutionContext(); | |
return executionContext == 'userinterface'; | |
} | |
function isZero(obj) { | |
return parseFloat(obj) == 0.0 | |
} | |
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_DISPUTE_ITEM_ITEM = 1311509; | |
function afterSubmit() { | |
var recordType = nlapiGetRecordType(); | |
var recordId = nlapiGetRecordId(); | |
processAfterSubmit(recordType, recordId); | |
} | |
function processAfterSubmit(recordType, recordId) { | |
if(recordType == 'creditmemo') { | |
// NOTE the entire record must be loaded to inspect the line items | |
var record = nlapiLoadRecord(recordType, recordId); | |
// NOTE credit memos from SuiteSync which override the default items on the invoice | |
// will only ever contain a single line item | |
// more information: https://dashboard.suitesync.io/docs/disputes#customizing-dispute-gl-impact | |
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_DISPUTE_ITEM_ITEM) { | |
modifyCreditMemo(record); | |
} | |
} else { | |
log("more than a single line item") | |
} | |
} else { | |
log("not credit memo, not processing") | |
} | |
} | |
function modifyCreditMemo(record) { | |
// NOTE Stripe charge description is pulled into the memo. Systems creating charges in Stripe | |
// normally create a structured description. You can use the structure of the memo to change the item. | |
var createdFromInvoiceId = record.getFieldValue('createdfrom'); | |
var invoice = nlapiLoadRecord('invoice', createdFromInvoiceId); | |
if(isEmpty(invoice)) { | |
error("error loading invoice"); | |
return | |
} | |
// TODO iterate over invoice and detect if a gift card was used | |
// TODO if so, modify the credit memo and include the gift card line item | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment