This code will integrate the items from the Netsuite to POS system on button click using client script.
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/*******************************************************************
* ClientScript
****************************************************************
*
* Date: 23/06/2022
*
* Author: Jobin and Jismi IT Services LLP
*
* REVISION HISTORY
*
* Revision 1.0
*
* Description: Script to send the item details to third party
*
* Revision 2.0
*
***************************************************************/
define(['N/currentRecord', 'N/http', 'N/url', 'N/record', 'N/email','N/runtime','N/search'],
function (currentRecord, http, url, record, email,runtime,searcgh) {
function pageInit(scriptContext) {
console.log("testing");
}
/**
* @description the fuction to send JSON object to POS API
* @param itemobj
* @return itemResponse
*/
function itemCreationPOSTAPICall(itemobj) {
try {
var headerObj = {
"X-APIKey": "4trgfdsfd243tg54342rewfcef",
"Content-Type": "application/json"
};
console.log("itemobj", itemobj);
var itemResponse = http.post({
//url: "http://160.242.52.34:9001/ProdMaster",
method: "POST",headers: headerObj,
body: JSON.stringify(itemobj)
});
var itemResponse="1";
//return itemResponse.body;
} catch (e) {
console.log("error", e);
return "";
}
}
/**
* @description the function create a search for finding the error response from the custom field
* @param id
* @return emailBody
*/
function ToBeEmailed(id) {
try {
var emailBody = '';
var itemSearchObj = search.create({
type: "item",
filters:
[
["type", "anyof", "Kit", "Service"],
"AND",
["custitem_jj_intgnresponse", "isnot", "\"1\""],
"AND",
["internalid", "anyof", id]
],
columns:
[
search.createColumn({name: "custitem_jj_intgnresponse", label: "Integration Response"})
]
});
var searchResultCount = itemSearchObj.runPaged().count;
if (searchResultCount > 0) {
itemSearchObj.run().each(function (result) {
emailBody = result.getValue(itemSearchObj.columns[1])
return true;
});
return emailBody
}
} catch (e) {
console.log("error", e)
}
}
/**
* @description the fuctcion to create item object to send to POS system
* @param newItem - Item object sending from UE
* @return {Object}item details
*/
function itemObjectGeneration(newItem,itemRecId) {
var itemobj = {
"PROD_ID": itemRecId?.toString(),
"PROD_NM": newItem.itemName?.toString(),
"PROD_NM1": newItem.productName1? newItem.productName1?.toString():"",
"PROD_GR": newItem.productGR?.toString(),
"SALE_RATE": newItem.saleRate? newItem.saleRate?.toString():0,
"TAX_CD": "8744",
"STATUS": newItem.status ? "N" : "Y"
}
return itemobj;
}
/**
* @description the fuctcion to sent item detail to POS system
* @param {*} newItem
*/
function sendItemDetailstoPOS(newItem) {
try {
const itemRec = currentRecord.get();
var itemRecId = itemRec.id;
var currentUser=runtime.getCurrentUser().id
console.log("currentUser",currentUser)
var itemObj = itemObjectGeneration(newItem,itemRecId);
var itemDetailResponse = itemCreationPOSTAPICall(itemObj);
console.log("itemDetailResponse",itemDetailResponse)
var itemType = "";
if (newItem.type == 'kititem') {
itemType = record.Type.KIT_ITEM
} else if (newItem.type == 'serviceitem') {
console.log("testtype")
itemType = record.Type.SERVICE_ITEM
}
var itemRecord = record.load({
type: itemType,
id: itemRecId
});
if (itemDetailResponse) {
itemRecord.setValue({
fieldId: 'custitem_jj_intgnresponse_rmce16',
value:itemDetailResponse,
ignoreFieldChange: true
});
}
itemRecord.save(({ignoreMandatoryFields:true, enableSourcing:true}));
//window.location.reload();
if(itemDetailResponse == '"1"'){
var emailBody=ToBeEmailed(itemRecId);
email.send({
author:currentUser,
recipients: 'litha.mukunthan@jobinandjismi.com',
subject: 'Error in integration of service and kit item',
body:emailBody,
});
}
} catch (e) {
log.debug('error', e);
}
}
return {
pageInit:pageInit,
sendItemDetailstoPOS: sendItemDetailstoPOS,
itemCreationPOSTAPICall:itemCreationPOSTAPICall,
itemObjectGeneration:itemObjectGeneration
};
});