Hunter Jacobs
03/14/2023, 8:33 PM/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
// Load Necessary Modules
define(['N/record', 'N/search'], function(record, search) {
//Update the last catalog date
function updateLastCatalog(customerId, today) {
const customerRecord = record.load({
type: record.Type.CUSTOMER,
id: customerId
});
customerRecord.setValue({
fieldId: 'custentity_last_order_date',
value: today
});
customerRecord.save();
}
function beforeSubmit(context) {
//Only run on sales orders being created over $500
if (context.type === context.UserEventType.CREATE && context.newRecord.type === record.Type.SALES_ORDER ) {
const newRecord = context.newRecord;
if (newRecord.getValue({
fieldId: 'subtotal'
}) < 500) {
return;
}
//Validate that this customer hasnt received a catalog in the last year
const customerId = newRecord.getValue({fieldId: 'entity'});
const customerInfo = search.lookupFields({
type: search.Type.CUSTOMER,
id: customerId,
columns: ['entityid', 'custentity_last_order_date', 'custentity_catalog_no_send']
});
const customerName = customerInfo.entityid;
const customerLastCatalogString = customerInfo.custentity_last_order_date;
const customerNoCatalog = customerInfo.custentity_catalog_no_send;
const today = new Date();
const customerLastCatalog = new Date(customerLastCatalogString)
const daysSinceLastCatalog = (customerLastCatalogString == "") ? 366 : Math.round((today - customerLastCatalog) / 86400000);
if ((customerName === 'Amazon Online Sales' || daysSinceLastCatalog > 365) && !customerNoCatalog) {
} else {
return;
}
//Go through each item to see what catalogs the order needs
let uniqueCatalogs = new Set();
const lineCount = newRecord.getLineCount({
sublistId: 'item'
});
for (let i = 0; i < lineCount; i++) {
const itemId = newRecord.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
//If a catalog is already on the order exit script
if (itemId === 8102 || itemId === 8101 || itemId === 2769 || itemId === 8103) {
updateLastCatalog(customerId, today)
return;
}
const itemCatalog = search.lookupFields({
type: search.Type.ITEM,
id: itemId,
columns: ['custitem_catalog_field']
}).custitem_catalog_field;
if (itemCatalog && itemCatalog.length > 0) {
uniqueCatalogs.add(itemCatalog[0].text);
}
}
//Exit script if no products have a catalog
if (uniqueCatalogs.size === 0){
return;
}
const catalogs = Array.from(uniqueCatalogs);
for (let i = 0; i < catalogs.length; i++) {
const currentLine = lineCount + i;
let currentCatalog = "";
let catalogDesc = catalogs[i] + " CATALOG";
switch (catalogs[i]) {
case "BODY SHOP":
currentCatalog = 8102;
break;
case "INDUSTRIAL":
currentCatalog = 8101;
break;
case "PROTECTIVE":
currentCatalog = 8103;
break;
case "NDT":
currentCatalog = 2769;
break;
default:
currentCatalog = 2769; //NDT & Other Catalog cannot exist -need to add NDT catalog Item
catalogDesc = catalogs[i] + " | Catalog Not On NS Script"
break;
}
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'item',
line: currentLine,
value: currentCatalog
});
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: currentLine,
value: 0
});
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: currentLine,
value: 1
});
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'description',
line: currentLine,
value: catalogDesc
});
}
updateLastCatalog(customerId, today)
}
}
return {
beforeSubmit: beforeSubmit
};
});
Nathan L
03/14/2023, 8:39 PM// instead of doing this
if (context.type === context.UserEventType.CREATE && context.newRecord.type === record.Type.SALES_ORDER ) {//do code stuff}
// try this
If (context.type !== context.UserEventType.CREATE || context.newRecord.type !== record.Type.SALES_ORDER) {return;}
// do code stuff
Nathan L
03/14/2023, 8:39 PMNathan L
03/14/2023, 8:41 PMif (!((customerName === 'Amazon Online Sales' || daysSinceLastCatalog > 365) && !customerNoCatalog)) {
return;
}
Nathan L
03/14/2023, 8:42 PMNathan L
03/14/2023, 8:43 PMNathan L
03/14/2023, 8:45 PMNathan L
03/14/2023, 8:46 PMHunter Jacobs
03/14/2023, 8:49 PMbattk
03/14/2023, 8:50 PMif (context.type === context.UserEventType.CREATE && context.newRecord.type === record.Type.SALES_ORDER ) {
is mostly unnecessary, you want to use the script deployment to filter on both the context type and the record typebattk
03/14/2023, 8:52 PMconst customerLastCatalog = new Date(customerLastCatalogString)
puts your code at risk when the user's date format doesnt match what the Date constructor expects, use _*format.parse*_ insteadbattk
03/14/2023, 8:54 PMconst itemCatalog = search.lookupFields({
type: search.Type.ITEM,
id: itemId,
columns: ['custitem_catalog_field']
}).custitem_catalog_field;
is unfavorable when there are multiple lines, either loop through the items and do 1 search to get all of their catalog fieldsbattk
03/14/2023, 8:55 PMbattk
03/14/2023, 8:59 PMfor (let i = 0; i < catalogs.length; i++) {
const currentLine = lineCount + i;
let currentCatalog = "";
let catalogDesc = catalogs[i] + " CATALOG";
switch (catalogs[i]) {
case "BODY SHOP":
currentCatalog = 8102;
break;
case "INDUSTRIAL":
currentCatalog = 8101;
break;
case "PROTECTIVE":
currentCatalog = 8103;
break;
case "NDT":
currentCatalog = 2769;
break;
default:
currentCatalog = 2769; //NDT & Other Catalog cannot exist -need to add NDT catalog Item
catalogDesc = catalogs[i] + " | Catalog Not On NS Script"
break;
}
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'item',
line: currentLine,
value: currentCatalog
});
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: currentLine,
value: 0
});
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: currentLine,
value: 1
});
newRecord.setSublistValue({
sublistId: 'item',
fieldId: 'description',
line: currentLine,
value: catalogDesc
});
}
this looks a little weird, it replaces the next line with your description? itembattk
03/14/2023, 9:00 PM