Jira Code: DAZ-29
To create, update, delete products and variants in Shopify when triggered in NetSuite.
1.Creation of Products/Variants .
To create an item in Shopify requires a parent product in Shopify only after which a variant can be created for the product. When a product is created in NetSuite a User event Script is deployed that run to send a POST request to create a product in Shopify and saves the product id of Shopify to NetSuite as external id. Once a product is made in Shopify, it automatically becomes the default variant. When variants are created for the same Product in NetSuite, User event Script is deployed that run to send a POST request to create a variant for the corresponding product in Shopify, saves the product id of Shopify to NetSuite as external id.
2. Update of Products/Variants.
When a product or variant is updated in NetSuite, a User event Script is deployed that run to send a PUT request to update a product/variant in Shopify using the external id saved in NetSuite.
3.Delete Products/Variants .
When a product or variant is deleted in NetSuite, a User event Script is deployed that run to send a DELETE request to update a product/variant in Shopify using the external id saved in NetSuite.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/*******************************************************************************
* DAZ-22 UE Stock update in Shopify From Netsuite
* **************************************************************************
*
* Date: 17-05-2019
*
* Author: Jobin & Jismi IT Services LLP
*
*****************************************************************************
**/
define(['N/file', 'N/http', 'N/https', 'N/record', 'N/runtime', 'N/search'],
function(file, http, https, record, runtime, search) {
var item_Eid;
var item_Id;
var item_Obj = {};
var main = {
afterSubmit: function(scriptContext) {
item_Id = scriptContext.newRecord.id;
//var item_Obj = {};
if (scriptContext.type == "create") {
item_Id = scriptContext.newRecord.id;
/*log.debug("item_Id:", item_Id);*/
var item_Subitem = scriptContext.newRecord.getValue({
fieldId: 'parent'
});
//FIRST ITEM/PRODUCT
if ((item_Subitem == null) || (item_Subitem == "") || (item_Subitem == undefined)) {
item_Obj.item_productID = null;
item_Obj.item_Subitem = null;
} else
item_Obj.item_Subitem = item_Subitem; //parent item
if (item_Obj.item_Subitem) { //if we have parent item
item_Subitem = item_Subitem.toString();
/*log.debug('item_Subitem', item_Subitem);*/
var itemSearchObj = search.create({
type: "item",
filters: [
["type", "anyof", "InvtPart", "NonInvtPart"],
"AND",
["internalidnumber", "equalto", item_Subitem],
"AND",
["externalidstring", "isnotempty", ""]
],
columns: [
search.createColumn({ name: "externalid", label: "External ID" })
]
});
var externalID
var searchResultCount = itemSearchObj.runPaged().count;
/*log.debug("itemSearchObj result count", searchResultCount);*/
itemSearchObj.run().each(function(result) {
externalID = result.getValue(itemSearchObj.columns[0]);
return false;
});
/*log.debug("externalID", externalID);*/
if (externalID) {
if (externalID.indexOf("/") > -1) {
res = externalID.split("/")
item_Obj.item_productID = res[0];
/*log.debug('item_Obj.item_productID', item_Obj.item_productID)*/
}
}
}
main.getData(scriptContext);
var prod_variantId = main.create_Item(item_Obj);
record.submitFields({
type: record.Type.INVENTORY_ITEM,
id: item_Id,
values: {
externalid: prod_variantId
},
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
log.debug("CREATED");
} else if (scriptContext.type == "delete") {
item_Eid = scriptContext.newRecord.getValue({
fieldId: 'externalid'
});
item_Eid = item_Eid.toString();
if (item_Eid.indexOf("/") > -1) {
res = item_Eid.split("/")
item_Obj.item_productID = res[0];
item_Obj.item_variantID = res[1];
} else {
item_Obj.item_productID = null;
item_Obj.item_variantID = item_Eid;
}
main.delete_Item(item_Obj);
} else if (scriptContext.type == "edit") {
item_Eid = scriptContext.newRecord.getValue({
fieldId: 'externalid'
});
item_Eid = item_Eid.toString();
if (item_Eid.indexOf("/") > -1) {
res = item_Eid.split("/")
item_Obj.item_productID = res[0];
item_Obj.item_variantID = res[1];
} else {
item_Obj.item_productID = null;
item_Obj.item_variantID = item_Eid;
}
main.getData(scriptContext);
main.edit_Item(item_Obj);
log.debug("EDITED");
}
},
create_Item: function(item_obj) {
/*log.debug('create obj', item_obj)*/
if (item_obj.item_productID) {
//creating variant item
var Item_response = https.post({
url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/products/' + item_obj.item_productID + '/variants.json',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
"variant": {
"sku": item_obj.item_Sku,
"option1": item_obj.item_Displayname,
"price": item_obj.item_Price,
"inventory_management": "shopify",
"inventory_quantity":item_Obj.quantityavail_Sum
}
})
});
var Item_content = JSON.parse(Item_response.body);
log.debug("Item_content", Item_content);
var result =""+Item_content.variant.id;
log.debug("Created Product/Variant with id", result);
return result;
} else { //creating product item
var Item_response = https.post({
url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/products.json',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
"product": {
"title": item_obj.item_Displayname,
"body_html": "<strong>Good Product!</strong>",
"vendor": item_obj.vendor_Name,
"product_type": "Accessories",
"tags": "eLiquid, Fruit, Iced, Reds eJuice",
"variants": [{
"price": item_obj.item_Price,
"sku": item_obj.item_Sku,
"option1": item_obj.item_Displayname,
"inventory_management": "shopify",
"inventory_quantity":item_Obj.quantityavail_Sum
}]
}
})
});
var Item_content = JSON.parse(Item_response.body);
log.debug("Item_content", Item_content);
/*log.debug("Item_content variant", Item_content.product.variants[0].id);*/
var result = Item_content.product.id + '/' + Item_content.product.variants[0].id
log.debug("Created Variant with id", result);
return result;
}
},
edit_Item: function(item_Nsdetails) {
if (item_Nsdetails.item_productID) {
var Item_response = https.put({
url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/products/'+item_Nsdetails.item_productID+'.json',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
"product": {
"id":item_Nsdetails.item_productID,
"title":item_Nsdetails.item_Name,
"body_html": "<strong>Good Product!</strong>",
"vendor": item_Nsdetails.vendor_Name,
"product_type": item_Nsdetails.item_type,
"tags": "eLiquid, Fruit, Iced, Reds eJuice",
}
})
});
var Item_content = JSON.parse(Item_response.body);
log.debug("Updated Product With id:",item_Nsdetails.item_productID);
}
var Item_response = https.put({
url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/variants/'+item_Nsdetails.item_variantID+'.json',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
"variant": {
"id": item_Nsdetails.item_variantID,
"sku": item_Nsdetails.item_Sku,
"option1":item_Nsdetails.item_Displayname,
"price": item_Nsdetails.item_Price,
"inventory_management": "shopify",
"inventory_quantity":item_Nsdetails.quantityavail_Sum
,
}
})
});
var Item_content = JSON.parse(Item_response.code);
log.debug("Updated Variant With id:",item_Nsdetails.item_variantID);
},
delete_Item: function(item_Nsdetails) {
item_Nsdetails.item_productID;
item_Nsdetails.item_variantID;
if (item_Nsdetails.item_productID) {
var Item_response = https.delete({
url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/products/' + item_Nsdetails.item_productID + '.json',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
}
});
var Item_content = JSON.parse(Item_response.body);
log.debug("Item_content", Item_content);
log.debug("Deleted Product/variant with id:",item_Nsdetails.item_productID+"/"+item_Nsdetails.item_variantID);
} else {
var Item_response = https.delete({
url: 'https://bfbb2c59c5c96793ca4acddc69ac3e70:c3d1104a816cd8b3ee224b2697ae266d@7daze-manufacturing.myshopify.com/admin/api/2019-04/variants/' + item_Nsdetails.item_variantID + '.json',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
}
});
var Item_content = JSON.parse(Item_response.body);
log.debug("Item_content", Item_content);
log.debug("Deleted Variant with id:",item_Nsdetails.item_variantID);
}
},
getData: function(scriptContext) {
/* var item_Sku = scriptContext.newRecord.getText({
fieldId: 'itemid'
});*/
item_Id = scriptContext.newRecord.id;
/*log.debug('item_Id', item_Id);*/
var itemRec = record.load({
type: record.Type.INVENTORY_ITEM,
id: item_Id,
isDynamic: false
})
var item_Id1 = scriptContext.newRecord.getValue({
fieldId: 'itemid'
});
item_Obj.item_Sku = item_Id;
var item_Price = itemRec.getSublistValue({
sublistId: 'price',
fieldId: 'price_1_',
line: 0
});
item_Obj.item_Price = item_Price;
var item_Displayname = scriptContext.newRecord.getValue({
fieldId: 'displayname'
});
item_Obj.item_Displayname = item_Displayname;
var item_Name = scriptContext.newRecord.getValue({
fieldId: 'itemid'
});
item_Obj.item_Name = item_Name;
var item_Upccode = scriptContext.newRecord.getValue({
fieldId: 'upccode'
});
item_Obj.item_Upccode = item_Upccode;
var vendor_Name = scriptContext.newRecord.getValue({
fieldId: 'vendorname'
});
item_Obj.vendor_Name = vendor_Name;
var stock_Description = scriptContext.newRecord.getValue({
fieldId: 'stockdescription'
});
item_Obj.stock_Description = stock_Description;
var quantityavail_Sum=0;
var locationCount = itemRec.getLineCount({
sublistId: 'locations'
});
for (var x = 0; x < locationCount; x++) {
var location = itemRec.getSublistValue({
sublistId: 'locations',
fieldId: 'location_display',
line: x
});
var quantityAvail = itemRec.getSublistValue({
sublistId: 'locations',
fieldId: 'quantityavailable',
line: x
});
quantityavail_Sum= quantityavail_Sum+quantityAvail;
}
item_Obj.quantityavail_Sum = quantityavail_Sum;
}
}
/* for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
};*/
function trycatch(myfunction, key) {
return function() {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("e in " + key, e);
}
}
};
return main;
});