Build order Script

How to create a sales orders in NetSuite with the sales order type as Build Order.

/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/

/********************************************
* * Build Order and Item Management Script
*
* ******************************************************
*
* Author: Jobin and Jismi IT Services
*
* Date Created :04 -January-2023
*
* Created By: Jobin and Jismi IT Services
*
* Description : Build Order and Item Management Script
*
* REVISION HISTORY
*
**********************************************************/
define(['N/currentRecord', 'N/record', 'N/search'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
*/
function (currentRecord, record, search) {


const salesOrderTypeObject = {
REGULAR_ORDER: 1,
REGULAR_ORDER_S: '1',
BUILD_ORDER: 2,
BUILD_ORDER_S: '2',
ONLINE_ORDER: 3,
ONLINE_ORDER_S: '3'
}
const EXCHANGE_SALES_ORDER_FORM = {
EXCHANGE_SO_FORM: 197,
EXCHANGE_SO_FORM_S: '197'
}

const TradePartnerSalesPrice = {
TRADE_PARTNER: 3,
TRADE_PARTNER_S: '3'
}
var itemIdArray =[];

/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/


function pageInit(scriptContext) {

}

/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
function fieldChanged(scriptContext) {

}

/**
* Validation function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @returns {boolean} Return true if field is valid
*
* @since 2015.2
*/
function validateField(scriptContext) {

try {

var currentRecord = scriptContext.currentRecord;
var isCustomerTradePartner = currentRecord.getValue({
fieldId: 'custbody_jj_is_cut_trade_partner'
});

var isEmployeeTradePartner = currentRecord.getValue({
fieldId: 'custbody_jj_is_employee_trade_part'
});


if (scriptContext.fieldId === 'custbody52') {

if (isCustomerTradePartner === false || isEmployeeTradePartner === false) {
var soType = currentRecord.getValue({
fieldId: 'custbody52'
});

if (soType === salesOrderTypeObject.BUILD_ORDER || soType === salesOrderTypeObject.BUILD_ORDER_S) {

confirm("You dont have permission to select the sales order type as 'Builder Order/Trade Partner Order'");
currentRecord.setValue({
fieldId: 'custbody52',
value: salesOrderTypeObject.REGULAR_ORDER
});
return false;
}
}
}

//**code by Jesna
//*******************************************************
// var recId = currentRecord.id
//
// if ((scriptContext.sublistId === 'item') && (scriptContext.fieldId === 'item')) {
//
// if (recId == "") {
// var salesPerson = currentRecord.getValue({
// fieldId: "custbody45"
// })
// var itemValue = currentRecord.getCurrentSublistValue({
// sublistId: 'item',
// fieldId: 'item'
// });
//
// if (itemValue !== "") {
// var checkInventoryItem = inventoryItemCheck(itemValue)
// console.log("checkInventoryItem", checkInventoryItem)
// if (checkInventoryItem === true) {
//
// var emplCertifcateSearch = employeeCertifcateSearch(salesPerson)
//
//
//
// var itemCertificateSearch = itemRecCertificateSearch(itemValue)
//
// if ((emplCertifcateSearch.length > 0) && (itemCertificateSearch.length > 0)) {
//
// if (emplCertifcateSearch.indexOf(itemCertificateSearch[0].getText({
// name: "custitem_jj_certificate_ahap_409",
// label: "Certification"
// })) !== -1) {
// } else {
// alert("Sales Associate does not have enough certification to add this item!")
// return false
// }
//
// } else {
// alert("Sales Associate does not have enough certification to add this item!")
// return false
// }
//
// }
//
// }
// }
//
// }
//*********************************************************************
return true;
} catch (e) {
console.log("error@validateField", e)
}

}

/**
* Validation function to be executed when sublist line is committed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateLine(scriptContext) {


try {

var currentRecord = scriptContext.currentRecord;
var salesOrderType = currentRecord.getValue({
fieldId: 'custbody52'
});
var quantity = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
});
var priceLevel = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'price'
});
var salesOrderForm = currentRecord.getValue({
fieldId: 'customform'
});

var isExchangeOrder = currentRecord.getValue({
fieldId: 'custbody_exchange_order'
})
var itemInternalID = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});


if (scriptContext.sublistId === 'item') {
console.log("validate lIne...........")
if (salesOrderType === salesOrderTypeObject.REGULAR_ORDER || salesOrderType === salesOrderTypeObject.REGULAR_ORDER_S || salesOrderType === salesOrderTypeObject.ONLINE_ORDER ||
salesOrderType === salesOrderTypeObject.ONLINE_ORDER_S) {

console.log("....item Id in validdate line...",itemInternalID);
var checkInventoryItem = inventoryItemCheck(itemInternalID)
console.log("checkInventoryItem..........",checkInventoryItem)

if(checkInventoryItem === true){
if(itemIdArray.indexOf(itemInternalID) === -1){
itemIdArray.push(itemInternalID);
console.log("itemIdArray..........",itemIdArray)
}
if (quantity > 10) {
confirm(" Please select the line level quantity less than 10 for Regular,Online and Exchange Orders");
return false;
}
}
}
if (salesOrderForm === EXCHANGE_SALES_ORDER_FORM.EXCHANGE_SO_FORM || salesOrderForm === EXCHANGE_SALES_ORDER_FORM.EXCHANGE_SO_FORM_S || isExchangeOrder === true) {
var checkInvItem = inventoryItemCheck(itemInternalID)
if(checkInvItem === true){
if (quantity > 10) {
confirm(" Please select the line level quantity less than 10 for Regular,Online and Exchange Orders");
return false;
}
}

}
if (salesOrderType === salesOrderTypeObject.REGULAR_ORDER || salesOrderType === salesOrderTypeObject.REGULAR_ORDER_S || salesOrderType === salesOrderTypeObject.ONLINE_ORDER ||
salesOrderType === salesOrderTypeObject.ONLINE_ORDER_S) {
if (priceLevel === TradePartnerSalesPrice.TRADE_PARTNER || priceLevel === TradePartnerSalesPrice.TRADE_PARTNER_S) {

confirm("You dont have permission to select the price level 'Trade Partner' in the line level");
return false;
}
}

}
return true

} catch (err) {
console.log("error@validateLine", err)
}

}


/**
* Validation function to be executed when record is saved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @returns {boolean} Return true if record is valid
*
* @since 2015.2
*/
function saveRecord(scriptContext) {


try {

var currentRecord = scriptContext.currentRecord;
var salesOrderType = currentRecord.getValue({
fieldId: 'custbody52'
})
var isCustomerTradePartner = currentRecord.getValue({
fieldId: 'custbody_jj_is_cut_trade_partner'
});

var isEmployeeTradePartner = currentRecord.getValue({
fieldId: 'custbody_jj_is_employee_trade_part'
});

var lineCount = currentRecord.getLineCount({
sublistId: 'item'
});
var flag = 0;
var flag1 = 0;
var lineArray = [];
var lineNumberArray = [];
var itemArray =[];

if (salesOrderType === salesOrderTypeObject.REGULAR_ORDER || salesOrderType === salesOrderTypeObject.REGULAR_ORDER_S || salesOrderType === salesOrderTypeObject.ONLINE_ORDER ||
salesOrderType === salesOrderTypeObject.ONLINE_ORDER_S) {

for (var i = 0; i < lineCount; i++) {

var price = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'price',
line: i,
})
var quantitys = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i,
})
var itemID = currentRecord.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i,
})
itemArray.push(itemID);

if (price === TradePartnerSalesPrice.TRADE_PARTNER || price === TradePartnerSalesPrice.TRADE_PARTNER_S) {
flag = flag + 1
lineArray.push(i + 1);
}
if (quantitys > 10) {

flag1 = flag1 + 1
lineNumberArray.push(i + 1);
}


}
var isInventory = inventoryItemSearch(itemArray)
console.log("isInventory..........",isInventory)

if (flag > 0) {
confirm("You have chosen the price level Trade Partner in the line numbers: " + lineArray + " Please change the Price Level values");
return false;
} else if (isInventory) {

if(flag1 > 0){
confirm("You have chosen the line level quantity greater than 10 in the following line number: " + lineNumberArray + " Please change the quantity.");
return false;
}
else
{
return true
}

} else {
return true;
}
} else if (salesOrderType === salesOrderTypeObject.BUILD_ORDER || salesOrderType === salesOrderTypeObject.BUILD_ORDER_S) {


if (isCustomerTradePartner === false || isEmployeeTradePartner === false) {
confirm("You dont have permission to choose the sales order type as 'Build Order'");
currentRecord.setValue({
fieldId: 'custbody52',
value: salesOrderTypeObject.REGULAR_ORDER
});
return false;
} else {
return true
}

} else {
return true;
}


} catch (err) {
console.log("error@saveRecord", err)
}

}

//Code by JJ0164 Item Management AHAP-409
function employeeCertifcateSearch(intId) {
try {
var employeeSearchObj = search.create({
type: "employee",

// filters:
// [
// ["custentity_empl_certification_ahap_409","anyof","1","2","3","4"],
// "AND",
// ["internalid","anyof",intId]
// ],
// columns:
// [
// search.createColumn({name: "internalid", label: "Internal ID"}),
// search.createColumn({
// name: "custentity_empl_certification_ahap_409",
// sort: search.Sort.ASC,
// label: "Certification"
// }),
// search.createColumn({
// name: "formulatext",
// formula: "CASE WHEN INSTR({custentity_empl_certification_ahap_409},'Core')>0 THEN 'Yes' ELSE '' END",
// label: "Core"
// }),
// search.createColumn({
// name: "formulatext",
// formula: "CASE WHEN INSTR({custentity_empl_certification_ahap_409},'Luxury')>0 THEN 'Yes' ELSE '' END",
// label: "Luxury"
// }),
// search.createColumn({
// name: "formulatext",
// formula: "CASE WHEN INSTR({custentity_empl_certification_ahap_409},'RIGGS')>0 THEN 'Yes' ELSE '' END",
// label: "RIGGS"
// }),
// search.createColumn({
// name: "formulatext",
// formula: "CASE WHEN INSTR({custentity_empl_certification_ahap_409},'GAGG')>0 THEN 'Yes' ELSE '' END",
// label: "GAGG"
// })
// ]

filters:
[
["internalid","anyof",intId],
"AND",
["custentity_empl_certification_ahap_409","anyof","1","2","3","4"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "custentity_empl_certification_ahap_409", label: "Certification"})
]

// filters:
// [
// ["internalid", "anyof", intId],
// "AND",
// [["custentity_aha_core", "is", "T"], "OR", ["custentity_aha_pro", "is", "T"], "OR", ["custentity_aha_riggs", "is", "T"], "OR", ["custentity_aha_gagg", "is", "T"]]
// ],
// columns:
// [
// // search.createColumn({name: "internalid", label: "Internal ID"}),
// search.createColumn({name: "custentity_aha_core", label: "Core"}),
// search.createColumn({name: "custentity_aha_pro", label: "Luxury"}),
// search.createColumn({name: "custentity_aha_riggs", label: "RIGGS"}),
// search.createColumn({name: "custentity_aha_gagg", label: "GAGG"})
//
// ]
});
var searchResult = employeeSearchObj.run().getRange({
start: 0,
end: 1
})


var empCertficateArray = []
if (searchResult.length > 0) {

for(var count=0;count<searchResult.length;count++){
console.log("searchResult...mmm.....",searchResult[count].getText({name: "custentity_empl_certification_ahap_409", label: "Certification"}))

var certficteNames=searchResult[count].getText({name: "custentity_empl_certification_ahap_409", label: "Certification"})

console.log("certficteNames",certficteNames)
var certificationList
certificationList=certficteNames.split(",")
console.log("certificationList...sdfs....",certificationList)
}
// for (var colummNo = 0; colummNo < searchResult[0].columns.length; colummNo++) {
// console.log("searchResult...mmm.....",searchResult[colummNo].getText({name: "custentity_empl_certification_ahap_409", label: "Certification"}))
//
// var certficteNames=searchResult[colummNo].getText({name: "custentity_empl_certification_ahap_409", label: "Certification"})
//
// console.log("certficteNames",certficteNames)
// console.log("certficteNames split",certficteNames.split(","))
// // var certficateSplit=certficteNames.split(",")
// var certificationList=[]
// certificationList=certficteNames.split(",")
//
// // if (searchResult[0].getValue(searchResult[0].columns[colummNo]) == true) {
// //
// // empCertficateArray.push(searchResult[0].columns[colummNo].label)
// // }
// }

console.log("certificationList",certificationList)
return certificationList
// return empCertficateArray
} else {
return []
}

} catch (e) {
console.log("error@itemManagement", e)
}
}


function itemRecCertificateSearch(itemId) {
try {
var inventoryitemSearchObj = search.create({
type: "inventoryitem",
filters:
[
["type", "anyof", "InvtPart"],
"AND",
["internalid", "anyof", itemId],
"AND",
["custitem_jj_certificate_ahap_409", "anyof", "1", "2", "3", "4"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "custitem_jj_certificate_ahap_409", label: "Certification"})
]
});
var searchResult = inventoryitemSearchObj.run().getRange({
start: 0,
end: 1
})

if (searchResult.length > 0) {
return searchResult
} else {
return []
}

} catch (e) {
console.log("error@itemRecCertificateSearch", e)
}
}

function inventoryItemCheck(itemId) {
try {
var inventoryitemSearchObj = search.create({
type: "inventoryitem",
filters:
[
["type", "anyof", "InvtPart"],
"AND",
["internalid", "anyof", itemId]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "type", label: "Type"})
]
});
var searchResult = inventoryitemSearchObj.run().getRange({
start: 0,
end: 1
})

if (searchResult.length > 0) {
return true
} else {
return false
}
} catch (e) {
console.log("error@inventoryItemCheck", e)
}
}


function inventoryItemSearch(itemArray){

try{

var inventoryitemSearchObj = search.create({
type: "inventoryitem",
filters:
[

["type","anyof","InvtPart"],
"AND",
["internalid","anyof",itemArray],

],
columns:
[

search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount1 = inventoryitemSearchObj.runPaged().count;

if(searchResultCount1 > 0){
return true
}
else
{
return false
}

}
catch(err){
console.log("error@inventoryItemSearch",err)
}
}

return {
pageInit: pageInit,
//fieldChanged: fieldChanged,
validateField: validateField,
validateLine: validateLine,
saveRecord: saveRecord
};

});

Leave a comment

Your email address will not be published. Required fields are marked *