Need to export all inventory and assembly items from NetSuite and import it into CEVA. This will be real time integration. So, whenever the inventory and assembly items created in NS, then it will be reflected in CEVA using a user event script.
The User Event script is given below:
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/https', 'N/record', 'N/recordContext', 'N/redirect', 'N/render', 'N/runtime', 'N/search', 'N/xml','N/ui/serverWidget'],
/**
* @param{https} https
* @param{record} record
* @param{recordContext} recordContext
* @param{redirect} redirect
* @param{render} render
* @param{runtime} runtime
* @param{search} search
* @param{xml} xml
* @param{serverWidget}serverWidget
*/
(https, record, recordContext, redirect, render, runtime, search, xml,serverWidget) => {
/**
* Post request to get the access token.
*/
function getAccessToken() {
var client_id = "1itPZCONiWr6N71I6wgVc99QjS4a";
var client_secret = "G1gfC6dlXqSF7gd4FfGtWeXXNOga";
var headers = [];
headers['content-type'] = 'application/x-www-form-urlencoded';
headers['accept'] = '*/*';
headers['user-agent'] = 'Auth gem v0.4.4';
headers['connection'] = 'close';
headers['content-length'] = '175';
var response = https.post({
url: 'https://apim-gw-uat.cevalogistics.com/token',
body: 'grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret,
headers: headers
});
var tokenObj = JSON.parse(response.body)
var access_token = tokenObj.access_token;
var token_type = tokenObj.token_type
return tokenObj;
}
/**
* API call to get the Inventory details from Ceva
* @param {object} tokenObj - details of access token
* @returns {Object} - Details of each employee
*/
function APIcall(tokenObj, body) {
var headers = [];
headers['accept'] = '*/*';
headers['content-type'] = 'application/json'
headers['Authorization'] = tokenObj.token_type + " " + tokenObj.access_token;
var itemURL = 'https://apim-gw-uat.cevalogistics.com/wms/productmastermanagement/1.0.0/product_master_requests'
log.debug('itemurl', itemURL)
try {
var response = https.post({
url: itemURL,
headers: headers,
body: JSON.stringify(body
)
});
var responseObj = response
log.debug('responseObj', responseObj)
} catch (e) {
log.debug("error@APICall", e)
}
return responseObj
}
/**
* Defines the function definition that is executed before record is loaded.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @param {Form} scriptContext.form - Current form
* @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
* @since 2015.2
*/
const beforeLoad = (scriptContext) => {
try {
var ItemRecNew = scriptContext.newRecord;
//var itemname = ItemRecNew.getValue({id: 'itemid'});
var itemsync = ItemRecNew.getValue({fieldId: 'custitem_ft_ceva_item_sync'});
log.debug("itemsync",itemsync)
if (scriptContext.type == scriptContext.UserEventType.COPY) {
// Getting the new record on Item receipt
ItemRecNew.setValue({
fieldId: 'custitem_ft_ceva_item_sync',
value: false
});
ItemRecNew.setValue({
fieldId: 'custitemft_error_response',
value: null
});
}
if (scriptContext.type === "edit") {
log.debug("EDIT")
var form = scriptContext.form;
if (itemsync === true) {
log.debug("true")
var itemname = form.getField({id: 'itemid'});
itemname.updateDisplayType({
displayType: serverWidget.FieldDisplayType.DISABLED
});
log.debug("true 1")
}
}
} catch (error) {
log.debug("error@beforeLoad", error);
}
}
/**
* Defines the function definition that is executed before record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const beforeSubmit = (scriptContext) => {
}
/**
* Defines the function definition that is executed after record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const afterSubmit = (scriptContext) => {
try {
if (scriptContext.type === "create") {
var objRecordNew = scriptContext.newRecord
var itemName = objRecordNew.getValue({
fieldId: "itemid"
})
log.debug("itemname", itemName);
var description = objRecordNew.getValue({
fieldId: "displayname"
})
log.debug("description", description);
var width = objRecordNew.getValue({
fieldId: "custitem2"
})
log.debug("width", width);
var weight = objRecordNew.getValue({
fieldId: "custitem_gross_weight"
})
log.debug("weight", weight);
var height = objRecordNew.getValue({
fieldId: "custitem1"
})
log.debug("height", height);
var depth = objRecordNew.getValue({
fieldId: "custitem3"
})
log.debug("depth", depth);
var length = objRecordNew.getValue({
fieldId: "custitem_custom_length"
})
log.debug("length", length);
var body = {
"cevaClientID": "QIP727",
"cevaSiteID": "US-FON-01",
"productID": itemName,
"descriptions": [
{
"languageISO2": "EN",
"description": description
}
],
"packingHierarchy": [
{
"width": Number(width),
"height": Number(height),
"length": Number(length)
}
]
}
log.debug("body",body)
log.debug("scriptContext", scriptContext.type)
var token = getAccessToken();
log.debug("token", token)
var cevaItemData = APIcall(token, body)
log.debug("cevaItemData", cevaItemData)
var type = objRecordNew.getValue({
fieldId: 'itemtype'
});
if (type == "InvtPart") {
var itemRecord = record.load({
type: record.Type.INVENTORY_ITEM,
id: objRecordNew.id,
isDynamic: true
});
} else if (type == "Assembly") {
var itemRecord = record.load({
type: record.Type.ASSEMBLY_ITEM,
id: objRecordNew.id,
isDynamic: true
});
}
if (cevaItemData.code === 200) {
itemRecord.setValue({
fieldId: 'custitem_ft_ceva_item_sync',
value: true
})
} else {
itemRecord.setValue({
fieldId: 'custitemft_error_response',
value: cevaItemData.body
});
}
itemRecord.save();
}
// if (scriptContext.type === "edit") {
// if (scriptContext.type !== scriptContext.UserEventType.CREATE){
//
// var ErrorResponsefield = objRecordNew.getField({
// id: 'custitem_ft_ceva_item_sync',
// });
//
// ErrorResponsefield.updateDisplayType({
// displayType: serverWidget.FieldDisplayType.DISABLED
// });
//
// }
} catch
(e) {
log.debug("error@afterSubmit", e)
}
}
return {beforeLoad,afterSubmit}
});