Jira Code : BTN-602
Integration of RQ iQMetrix with NetSuite.
NetSuite item will be saved as an Electronic Product Catalogue in RQ.
NetSuite Sales Order will be Purchase Order in RQ.
NetSuite Purchase Order will be Advanced Shipment Notice in RQ.
Every sync details will be stored in a Custom Record for future reference as for resubmitting if any.
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/email', 'N/http', 'N/https', 'N/record', 'N/runtime', 'N/search', 'N/url', 'N/config'],
function (email, http, https, record, runtime, search, url, config) {
var main = {
execute: function (scriptContext) {
//to know whether production or sandbox
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
var ns_companyid = companyInfo.getValue({
fieldId: 'companyid'
});
//ns_companyid = parseInt(ns_companyid);
log.debug('ns_companyid = ', ns_companyid);
log.debug('ns_companyid ==4215023 ', ns_companyid == 4215023);
log.debug('ns_companyid !=4215023 ', ns_companyid != 4215023);
if (ns_companyid != 4215023 || ns_companyid != '4215023')
return false;
var recorddetails = main.getrecordsinternalid();
log.debug("recorddetails", recorddetails);
var statusrecord = [];
recorddetails.forEach(function (arrayItem) {
var status = JSON.parse(JSON.parse(arrayItem.epcresponse));
log.debug("status", status["Status"]);
var token = main.generatetoken();
log.debug("token", token)
var currentstatus = main.getthestatus(token, status["Status"]);
record.load({
type: "customrecord_btn_319_jj_iqmetrix_epc",
id: arrayItem.internalid
}).setValue({
fieldId: 'custrecord_btn_319_jj_iqmetrix_epc_batch',
value: currentstatus,
ignoreFieldChange: true
}).save();
});
},
getrecordsinternalid: function () {
var recorddetails = [];
var custrecord_btn_319_jj_iqmetrix_epc_batchstatusSearchObj = search.create({
type: "customrecord_btn_319_jj_iqmetrix_epc",
filters: [
//["custrecord_btn_319_jj_iqmetrix_epc_batch", "contains", "\"StatusName\":\"Pending\""]
["custrecord_btn_319_jj_iqmetrix_epc_batch", "contains", ""StatusName":"Pending""]
],
columns: [
search.createColumn({
name: "custrecord_btn_319_jj_iqmetrix_epc_all_i"
}),
search.createColumn({
name: "custrecord_btn_319_jj_iqmetrix_epc_respo"
}),
search.createColumn({
name: "internalid",
label: "Internal ID"
})
]
});
var searchResultCount = custrecord_btn_319_jj_iqmetrix_epc_batchstatusSearchObj.runPaged().count;
log.debug("custrecord_btn_319_jj_iqmetrix_epc_batchstatusSearchObj result count", searchResultCount);
custrecord_btn_319_jj_iqmetrix_epc_batchstatusSearchObj.run().each(function (result) {
var recordobj = {};
recordobj.internalid = result.getValue("internalid");
recordobj.epcresponse = result.getValue("custrecord_btn_319_jj_iqmetrix_epc_respo");
recorddetails.push(recordobj);
return true;
});
return recorddetails;
},
generatetoken: function () {
var staticIQMetrixObj = {
Environment: 'RC',
CompanyName: '-----------',
CompanyID: '----------',
UserName: '---------',
Password: '--------------',
SecurityRole: 'API Integration at ZIGSIT6',
ClientId: '------',
ClientSecret: '---------',
}
var REQUEST = '';
REQUEST = 'grant_type=password&username=' + staticIQMetrixObj.UserName + '&password=' + staticIQMetrixObj.Password + '&client_id=' + staticIQMetrixObj.ClientId + '&client_secret=' + staticIQMetrixObj.ClientSecret + '&';
var response = https.post({
url: 'https://accounts.iqmetrix.net/v1/oauth2/token',
body: REQUEST,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded'
}
});
log.debug("response", response);
return JSON.parse(response.body).access_token;
},
getthestatus: function (token, directory) {
log.debug("token", token);
log.debug("directory", directory);
var response = https.get({
url: 'https://platformepc.iqmetrix.net/' + directory,
headers: {
'Authorization': 'Bearer ' + token,
'Accept': 'application/json',
}
});
log.debug('Status response.body', response.body);
return response.body;
}
}
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;
});