REQUIREMENT
Develop an automation to populate the project code values in the expense report based on the provided conditions.
SOLUTION
The automation can be done by adding a userevent script that can populate the project code based on the conditions provided.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(["N/record", "N/search"], /**
* @param{record} record
* @param{search} search
*/ (record, search) => {
"use strict";
/**
* Defines the function definition that is executed after record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const afterSubmit = (scriptContext) => {
try {
let objRecord = scriptContext.newRecord;
let recordType = objRecord.type;
let newRecId = objRecord.id;
let transRecord = record.load({
type: recordType,
id: newRecId,
isDynamic: false,
});
//Expense Report
if (recordType == "expensereport") {
let linesCount = transRecord.getLineCount({
sublistId: "expense",
});
let expenseReportLinesWithoutLuxCode = lineWithoutProjectCodeExists(
transRecord,
linesCount,
"expense"
);
if (expenseReportLinesWithoutLuxCode.length > 0) {
let updateExpenseAccountExpenseReport =
updateExpenseAccountExpenseReports(
transRecord,
expenseReportLinesWithoutLuxCode
);
log.audit(
"updateExpenseAccountExpenseReport",
updateExpenseAccountExpenseReport
);
}
let expenseReportLinesWithoutLuxCodeDesc = lineWithoutProjectCodeExists(
transRecord,
linesCount,
"expense"
);
if (expenseReportLinesWithoutLuxCodeDesc.length > 0) {
let updateDescExpenseReport = updateDescExpenseReports(
transRecord,
expenseReportLinesWithoutLuxCodeDesc
);
log.audit("updateDescExpenseReport", updateDescExpenseReport);
}
}
let recordId = transRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true,
});
} catch (err) {}
};
function checkAccount(accountId) {
try {
let accountSearchObj = search.create({
type: "account",
filters: [
[
"internalid",
"anyof",
"1804",
"1812",
"1813",
"1814",
"1815",
"2649",
"2650",
"2734",
"1521",
],
"AND",
["internalid", "anyof", accountId],
],
columns: [
search.createColumn({
name: "name",
sort: search.Sort.ASC,
label: "Name",
}),
search.createColumn({ name: "number", label: "Number" }),
],
});
let searchResultCount = accountSearchObj.runPaged().count;
if (searchResultCount > 0) {
return true;
} else {
return false;
}
} catch (err) {
log.error("error@checkAccount", err);
return false;
}
}
function checkForParameter(parameter) {
if (
parameter !== "" &&
parameter !== null &&
parameter !== undefined &&
parameter !== false &&
parameter !== "null" &&
parameter !== "undefined" &&
parameter !== " " &&
parameter !== "false"
) {
return true;
}
}
function lineWithoutProjectCodeExists(transRecord, itemLineCount, recType) {
try {
//Get all the item lines without lux cost project code
let linesWithoutProjectCode = [];
for (let itm = 0; itm < itemLineCount; itm++) {
if (
transRecord.getSublistValue({
sublistId: recType,
fieldId: "csegprojectcode",
line: itm,
}) != 1
) {
linesWithoutProjectCode.push(itm);
}
}
return linesWithoutProjectCode;
} catch (err) {
log.error("error@lineWithoutProjectCodeExists", err);
}
}
function updateExpenseAccountExpenseReports(
transRecord,
expenseReportLinesWithoutLuxCode
) {
try {
for (
let lines = 0;
lines < expenseReportLinesWithoutLuxCode.length;
lines++
) {
let linesExpenseAccount = transRecord.getSublistValue({
sublistId: "expense",
fieldId: "expenseaccount",
line: expenseReportLinesWithoutLuxCode[lines],
});
if (checkAccount(linesExpenseAccount)) {
transRecord.setSublistValue({
sublistId: "expense",
fieldId: "csegprojectcode",
line: expenseReportLinesWithoutLuxCode[lines],
value: 1,
});
}
// }
}
} catch (err) {
log.error("updateExpenseAccountExpenseReports", err);
}
}
function updateDescExpenseReports(
transRecord,
expenseReportLinesWithoutLuxCodeDesc
) {
try {
for (
let lines = 0;
lines < expenseReportLinesWithoutLuxCodeDesc.length;
lines++
) {
let itemDescription = transRecord.getSublistValue({
sublistId: "expense",
fieldId: "memo",
line: expenseReportLinesWithoutLuxCodeDesc[lines],
});
if (checkForParameter(itemDescription)) {
//if (checkLineDescription(itemDescription, recordType)) {
if (itemDescription.toLowerCase().includes("ltm bonus")) {
transRecord.setSublistValue({
sublistId: "expense",
fieldId: "csegprojectcode",
line: expenseReportLinesWithoutLuxCodeDesc[lines],
value: 1,
});
let val = transRecord.getSublistValue({
sublistId: "expense",
fieldId: "csegprojectcode",
line: expenseReportLinesWithoutLuxCodeDesc[lines],
});
}
}
}
} catch (err) {}
}
return { afterSubmit };
});