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/bfe6f95464539be8ab26576665623fb4 to your computer and use it in GitHub Desktop.
Save iloveitaly/bfe6f95464539be8ab26576665623fb4 to your computer and use it in GitHub Desktop.
/*
Author: Mike Bianco <mike@suitesync.io>
Description: Generate a gift card code using an external service when a sales order is created
Link:
# Setup Instructions
## Upload Script
https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl
File Name: gift_card_generate_before_submit.js
## Create Script: User Event
Name: SalesOrder Gift Card Hook
ID: _giftcard_hook
Description: Generates a unique gift card code when the SalesOrder is created using an external service
Before Load: beforeLoad
Before Submit: beforeSubmit
Deployments:
CustomerRefund
customdeploy_suitesync_refund_user_event
Deployed: true
Execute in eCommerce: false
Status: Released
Execute as Administrator
## Create Script: Client
Name: SuiteSync Customer Refund Client
ID: _suitesync_refund_client
Page Init: pageInit
*/
function isUserInterfaceContext() {
var context = nlapiGetContext();
var executionContext = context.getExecutionContext();
return executionContext == 'userinterface';
}
function log(msg) {
nlapiLogExecution('DEBUG', msg);
}
function error(msg) {
nlapiLogExecution('ERROR', msg);
}
function isEmpty(obj) {
return obj === undefined || obj === null || obj === "";
}
// https://system.sandbox.netsuite.com/app/common/item/item.nl?id=26802
var NETSUITE_ELECTRONIC_GIFT_CARD_ID = 26802;
function afterSubmit(type) {
var transactionType = nlapiGetRecordType();
var transactionInternalId = nlapiGetRecordId();
if(transactionType != 'salesorder') {
error("gift card script run for sales order")
return;
}
var salesOrder = nlapiGetNewRecord();
processAfterSubmit(salesOrder);
}
function processAfterSubmit(salesOrder) {
log("processing sales order");
var lines = salesOrder.getLineItemCount('item');
var x = 1;
while(x <= lines) {
salesOrder.selectLineItem('item', x);
var itemInternalId = salesOrder.getCurrentLineItemValue('item', 'item');
if(parseInt(itemInternalId) == NETSUITE_ELECTRONIC_GIFT_CARD_ID) {
var giftCardCode = salesOrder.getCurrentLineItemValue('item', 'giftcertnumber')
if(isEmpty(giftCardCode)) {
var giftCardCode = getGiftCardCode();
salesOrder.setCurrentLineItemValue ('item', 'giftcertnumber', giftCardCode)
}
}
x++;
}
// iterate through line items
// determine if there is an egift cards
// check if a code has already been defined
// call out to gift card service to generate code
// update gift card code on the line item
}
function getGiftCardCode() {
var postdata = {};
var headers = {};
var context = nlapiGetContext();
var isSandboxEnvironment = context.getEnvironment() != 'PRODUCTION';
var stagingURL = 'https://staging-giftcard-service.com/code?type=electronic&s=SECRET';
var productionURL = 'https://staging-giftcard-service.com/code?type=electronic&s=SECRET';
var targetURL;
if(isSandboxEnvironment) {
targetURL = stagingURL;
} else {
targetURL = productionURL;
}
var response = nlapiRequestURL(targetURL, postdata, headers);
var giftCardCode;
try {
if(response.getCode() == 200) {
giftCardCode = response.getBody();
}
} catch(err) {
throw nlapiCreateError(
'GIFTCARD_ERROR',
'Gift card code is not valid',
true
);
}
return giftCardCode;
}
// https://system.sandbox.netsuite.com/app/accounting/transactions/salesord.nl?id=1467499
if(nlapiGetContext().getExecutionContext() == 'debugger') {
var salesOrder = nlapiLoadRecord('salesorder', 1467499);
processAfterSubmit(salesOrder)
}
'debug';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment