SCENARIO: On creating credit memo in netsuite, if the subsidiary is Canada, the credit memo data is pushed to numerik
Credit memo data should be neagtive
If corresponding customer is not is numerik, the customer is also pushed to numerik
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/*******************************************************************************
* Date : 19 July 2021
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : Pushing credit memo datas to numerik
*
******************************************************************************
* DEPLOYMENT :CREDITMEMO
* This script summarises on pushing credit memo datas to numerik
*
* SCENARIO: On creating credit memo in netsuite, if the subsidiary is Canada, the credit memo data is pushed to numerik
* Credit memo data should be neagtive
* If corresponding customer is not is numerik, the customer is also pushed to numerik
*
* *****************************************************************************
* REVISION HISTORY
*
* Revision 1.0 ${19 July 2021} JJ0131 : created
* Revision 2.0 ${28 Sep 2021} JJ0131 : Updated
*/
define(['N/https', 'N/record', 'N/search', 'N/email', 'N/runtime'],
/**
* @param{https} https
* @param{record} record
* @param{search} search
*/
(https, record, search, email, runtime) => {
const afterSubmit = (scriptContext) => {
try {
//Canada Subsidiaries internalid
var CASUB = 11,
CASUB2 = 14;
var crmemo = scriptContext.newRecord;
var crmemoid = crmemo.id;
log.debug("CRMEMO", crmemoid);
var header = [];
header['ApiKey'] = 'GcKVrpepGj1kwkIKNi5eNBNdnLuHgdEp3e972WrJ+qo=';
header['Content-Type'] = 'application/json';
if (scriptContext.type === scriptContext.UserEventType.CREATE) {
//On credit memo creation, the same will be pushed to numerik
var cmrecord = record.load({
type: record.Type.CREDIT_MEMO,
id: crmemoid,
isDynamic: true
});
var subs = cmrecord.getValue({fieldId: 'subsidiary'});
if (subs == CASUB2 || subs == CASUB) {
var cusid = cmrecord.getValue({fieldId: 'entity'});
var cusname = cmrecord.getText({fieldId: 'entity'});
/***** If customer is not present in numerik, push them*****/
function CusData() {
try {
var cusResponse = https.get({
url: 'https://numerik-publicapi-prod.azurewebsites.net/api/Customers',
headers: header
});
var cusObj = JSON.parse(cusResponse.body);
var cusPresent = false;
for (var i = 0; i < cusObj.length; i++) {
if (cusObj[i].id == cusid) {
cusPresent = true;
return cusPresent;
}
}
if (cusPresent == false) {
var cusrecord = record.load({
type: record.Type.CUSTOMER,
id: cusid,
isDynamic: true
});
var cusname = cusrecord.getValue({fieldId: 'companyname'});
var cusadr1 = cusrecord.getValue({fieldId: 'billaddr1'});
var cusadr2 = cusrecord.getValue({fieldId: 'billaddr2'});
var cusadr3 = cusrecord.getValue({fieldId: 'billaddr3'});
var cusemail = cusrecord.getValue({fieldId: 'email'});
var cusphone = cusrecord.getValue({fieldId: 'phone'});
var postcode = cusrecord.getValue({fieldId: 'billzip'});
var cuscity = cusrecord.getValue({fieldId: 'billcity'});
var cuscountry = cusrecord.getValue({fieldId: 'billcountry'});
var primarycontact = cusrecord.getValue('contact');
var cusalesrep = cusrecord.getText({fieldId: 'salesrep'});
var cusgrp1 = cusrecord.getText('custentity32');
var cusgrp2 = cusrecord.getText('custentity_distribution_channel');
var cusgrp4 = cusrecord.getText('custentity43');
var cusgrp6 = cusrecord.getValue('custentity44');
var cusgrp7 = cusrecord.getValue('parent');
var cusgrp8 = cusrecord.getText('custentity2');
var cusgrp9 = cusrecord.getText('custentity_rep_run');
var cusobj = [{}];
cusobj[0]["numerikId"] = 0;
cusobj[0]["id"] = cusid.toString();
cusobj[0]["name"] = cusname;
cusobj[0]["address1"] = cusadr1;
cusobj[0]["address2"] = cusadr2;
cusobj[0]["address3"] = cusadr3;
cusobj[0]["billToCity"] = cuscity;
cusobj[0]["postCode"] = postcode;
cusobj[0]["billToCountry"] = cuscountry;
cusobj[0]["email"] = cusemail;
cusobj[0]["phoneNumber"] = cusphone;
cusobj[0]["platinum"] = true;
cusobj[0]["consultant"] = primarycontact;
cusobj[0]["salesRep"] = cusalesrep;
cusobj[0]["customerGroup1"] = cusgrp1;
cusobj[0]["customerGroup2"] = cusgrp2; ////DISTRIBUTION CHANNEL
cusobj[0]["customerGroup3"] = '';
cusobj[0]["customerGroup4"] = cusgrp4; // Primary Group
cusobj[0]["customerGroup5"] = '';
cusobj[0]["customerGroup6"] = cusgrp6; //INSIDE SALES REP
cusobj[0]["customerGroup7"] = cusgrp7; // PARENT COMPANY
cusobj[0]["customerGroup8"] = cusgrp8; //Industry
cusobj[0]["customerGroup9"] = cusgrp9;
var response = https.post({
url: 'https://numerik-publicapi-prod.azurewebsites.net/api/Customers',
body: JSON.stringify(cusobj),
headers: header
});
log.debug("CODE", response.code);
} else {
log.debug("CUstomer is present");
}
} catch (e) {
log.debug("Error@cusdata", e.message);
log.error("Error@cusdata", e.message);
}
}
CusData();
CreditMemo(cmrecord, header);
}
}
if (scriptContext.type === scriptContext.UserEventType.DELETE) {
/**Delete Credit memo data if it is deleted from netsuite**/
try {
var tranid = crmemo.getValue({fieldId: 'tranid'});
var subs = crmemo.getValue({fieldId: 'subsidiary'});
if (subs == CASUB || subs == CASUB2) {
var crresponse = https.delete({
url: 'https://numerik-publicapi-prod.azurewebsites.net/api/SalesData/TransactionID/' + tranid,
headers: header
});
log.debug("DELETE CODE", crresponse.code);
}
} catch (e) {
log.debug("Error@delete", e.message);
log.error("Error@delete", e.message);
}
}
if (scriptContext.type === scriptContext.UserEventType.EDIT) {
/**When editing the credit memo is deleted and again pushed to numrik**/
try {
var cmrecord = record.load({
type: record.Type.CREDIT_MEMO,
id: crmemoid,
isDynamic: true
});
var subs = cmrecord.getValue({fieldId: 'subsidiary'});
if (subs == CASUB || subs == CASUB2) {
var tranid = crmemo.getValue({fieldId: 'tranid'});
//If credit memo data is present the data is deleted and repushed
var crresponse = https.get({
url: 'https://numerik-publicapi-prod.azurewebsites.net/api/SalesData/TransactionId/' + tranid,
headers: header
});
if (crresponse.code == 200) {
var crresponse = https.delete({
url: 'https://numerik-publicapi-prod.azurewebsites.net/api/SalesData/TransactionId/' + tranid,
headers: header
});
log.debug("DELETE CODE", crresponse.code);
}
CreditMemo(cmrecord, header);
}
} catch (e) {
log.debug("Error@edit", e.message);
log.error("Error@edit", e.message);
}
}
} catch (e) {
log.debug({title: e.name, details: e.message});
log.error({title: e.name, details: e.message});
}
}
/****Function to post credit memo datas to numerik****/
function CreditMemo(cmrecord, header) {
try {
var tranid = cmrecord.getValue({fieldId: 'tranid'});
var tranDate = cmrecord.getValue({fieldId: 'trandate'});
var numlines = cmrecord.getLineCount({sublistId: 'item'});
log.debug("numlines", numlines)
var salesrep = cmrecord.getText({fieldId: 'salesrep'});
var productObj = [];
var salesDataObj = [];
for (var i = 0; i < numlines; i++) {
var itemtype = cmrecord.getSublistValue({sublistId: 'item', fieldId: 'itemtype', line: i});
log.debug("Itemtype", itemtype)
if (itemtype !== 'Group' && itemtype !== 'EndGroup') {
var item = cmrecord.getSublistValue({sublistId: 'item', fieldId: 'item', line: i});
var qty = cmrecord.getSublistValue({sublistId: 'item', fieldId: 'quantity', line: i});
var line = cmrecord.getSublistValue({sublistId: 'item', fieldId: 'line', line: i});
log.debug('line', line);
if (line == "" || line == null) {
line = i + 1;
}
log.debug('line', line);
var amount = cmrecord.getSublistValue({sublistId: 'item', fieldId: 'amount', line: i});
if (amount == null || amount == "") {
amount = 0;
}
var costestimate = cmrecord.getSublistValue({
sublistId: 'item',
fieldId: 'costestimate',
line: i
});
var rate = cmrecord.getSublistValue({sublistId: 'item', fieldId: 'rate', line: i});
if (rate == "" || rate == null) {
rate = 0;
}
var cusname = cmrecord.getText({fieldId: 'entity'});
var cusid = cmrecord.getValue({fieldId: 'entity'});
var itemfields = search.lookupFields({
type: 'item',
id: item,
columns: ['itemid', 'displayname', 'custitem19']
});
if (costestimate == null || costestimate == '')
costestimate = 0;
var estgrossprofit = amount - costestimate;
var gross = estgrossprofit.toFixed(2);
if (gross > 0) {
gross = gross * -1;
}
if (amount > 0) {
amount = amount * -1;
}
var itemgroup = '';
if (itemfields.custitem19[0]) {
itemgroup = itemfields.custitem19[0].text;
}
var itemid = itemfields.itemid;
log.debug('itemname', itemid);
var itemdesc = itemfields.displayname;
var Obj = {};
Obj[i] = {};
Obj[i].numerikId = 0;
Obj[i].id = item.toString();
Obj[i].name = itemid;
Obj[i].description = itemdesc;
Obj[i].productGroup = itemgroup;
Obj[i].productManager = '';
productObj.push(Obj[i]);
var salesObj = {}
salesObj[i] = {};
salesObj[i].numerikId = 0;
salesObj[i].transactionId = tranid;
salesObj[i].dateOfTransaction = tranDate;
salesObj[i].lineId = line.toString();
salesObj[i].quantity = qty;
salesObj[i].unitSalesPrice = rate;
salesObj[i].lineTotal = amount;
salesObj[i].grossProfitDollars = gross;
salesObj[i].customerId = cusid;
salesObj[i].customerName = cusname;
salesObj[i].productId = itemid;
salesObj[i].productDescription = itemdesc;
salesObj[i].productGroup = itemgroup;
salesObj[i].salesRep = salesrep;
salesObj[i].consultant = ''; //transType
salesObj[i].projectId = '';
salesObj[i].projectName = '';
salesDataObj.push(salesObj[i]);
}
}
var prodresponse = https.post({
url: 'https://numerik-publicapi-prod.azurewebsites.net/api/Products',
body: JSON.stringify(productObj),
headers: header
});
log.debug("PRDCODE", prodresponse.code);
var salesresponse = https.post({
url: 'https://numerik-publicapi-prod.azurewebsites.net/api/SalesData',
body: JSON.stringify(salesDataObj),
headers: header
});
log.debug("SALECODE", salesresponse.code)
} catch (e) {
log.debug({title: "Error@creditmemo", details: e.message});
log.error({title: "Error@creditmemo", details: e.message});
var script = runtime.getCurrentScript();
var rec = script.getParameter({
name: 'custscript_jj_cm_err_email'
});
var user = runtime.getCurrentUser();
var auth = user.id;
var env = runtime.envType;
var acnt = runtime.accountId;
var scriptid = script.id;
email.send({
author: auth,
recipients: rec,
subject: 'Numerik Sync Failure: Credit Memo #' + tranid,
body: 'Account ID: ' + acnt + 'n' +
'Environment: ' + env + 'n' +
'Script: ' + scriptid + 'n' +
'Error Details: ' + e.name + 'n' +
'Error Message: ' + e.message
});
}
}
return {afterSubmit}
});