Created
March 31, 2017 13:29
-
-
Save iloveitaly/0ffc0863abdeb3c1b8b14fd989367d29 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: Scripts to copy Stripe IDs stored in multiple fields into a single field | |
// used by SuiteSync | |
// 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 === ""; | |
} | |
// Mass Update Setup | |
// 1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl | |
// * suitesync_field_mapping.js | |
// 2. Mass Update: | |
// * Label: SuiteSync [CashSale/CashRefund] Map IDs | |
// * ID: _suitesync[_cs/_cf]_map_stripe_ids | |
// * Function: massUpdate | |
// * Deployments: CashSale & CashRefund. Note that NetSuite will throw an error | |
// if you try to create two deployments at once. You need to create | |
// a separate Mass Update script for each record. | |
// * CashSale. ID: _suitesync_update_cashsale. Status: Released | |
// * CashRefund. ID: _suitesync_update_cashrefund. Status: Released | |
// User Event Setup | |
// 1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl | |
// * suitesync_field_mapping.js | |
// 2. User Event | |
// * Label: Unify Stripe ID | |
// * ID: _suitesync_unify_stripe_id | |
// * After Submit: afterSubmit | |
// * Deployments > CashSale, _suitesync_unify_cs_id, Released | |
// * Deployments > CashRefund, _suitesync_unify_cf_id, Released | |
// NOTE NS will throw an error if you try to create the deployments at the same time. | |
// you'll need to create them separately. | |
// Execution | |
// NOTE deployments must be released to appear in the Mass Update screen | |
// https://system.sandbox.netsuite.com/app/common/bulk/bulkops.nl | |
// Custom Updates: Cash Sale/Cash Refund | |
// Search | |
// * Type: CashSale | |
// * MainLine: true | |
// * Date: enter in a date range | |
var UNDEPOSITED_FUNDS_ID = 115; | |
var STRIPE_REFUND_ID_FIELD = 'custbody_stripe_refundid'; | |
var STRIPE_CHARGE_ID_FIELD = 'custbody_stripe_token'; | |
var UNIFIED_STRIPE_ID_FIELD = 'custbody_suitesync_authorization_code'; | |
function massUpdate(recordType, recordId) { | |
if(recordType == 'cashsale') { | |
unifyStripeIdForCashSale(recordId); | |
} else if(recordType == 'cashrefund') { | |
// NOTE refunds must be set to undeposited funds in order for them to show up in the deposit list! | |
nlapiSubmitField('cashrefund', recordId, "account", UNDEPOSITED_FUNDS_ID); | |
unifyStripeIdForCashRefund(recordId) | |
} else { | |
error("unsupported record type: " + recordType) | |
} | |
} | |
function afterSubmit() { | |
var recordType = nlapiGetRecordType(); | |
var recordId = nlapiGetRecordId(); | |
if(recordType == 'cashsale') { | |
unifyStripeIdForCashSale(recordId); | |
} else if(recordType == 'cashrefund') { | |
unifyStripeIdForCashRefund(recordId); | |
} | |
} | |
function unifyStripeIdForCashSale(cashSaleId) { | |
var stripeChargeId = nlapiLookupField('cashsale', cashSaleId, STRIPE_CHARGE_ID_FIELD, false); | |
if(!isEmpty(stripeChargeId)) { | |
var currentId = nlapiLookupField('cashsale', cashSaleId, UNIFIED_STRIPE_ID_FIELD, false); | |
if(currentId != stripeChargeId) { | |
nlapiSubmitField('cashsale', cashSaleId, UNIFIED_STRIPE_ID_FIELD, stripeChargeId); | |
} | |
} | |
} | |
function unifyStripeIdForCashRefund(cashRefundId) { | |
var stripeRefundId = nlapiLookupField('cashrefund', cashRefundId, STRIPE_REFUND_ID_FIELD, false); | |
if(!isEmpty(stripeRefundId)) { | |
var currentId = nlapiLookupField('cashrefund', cashRefundId, UNIFIED_STRIPE_ID_FIELD, false); | |
if(currentId != stripeRefundId) { | |
nlapiSubmitField('cashrefund', cashRefundId, UNIFIED_STRIPE_ID_FIELD, stripeRefundId); | |
} | |
} | |
} | |
if(nlapiGetContext().getExecutionContext() == 'debugger') { | |
massUpdate('cashrefund', 2140523) | |
} | |
'debug'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment