This article provides an in-depth look at how to efficiently retrieve and cache project codes from a NetSuite custom record, and how to group them by department. The code includes error handling and logging to ensure reliability and traceability.
Retrieving the List of Project Codes
The getProjectCodeList method aims to fetch a list of project codes. It uses a caching mechanism to optimize performance by reducing the number of times the data is fetched from the original source.
Code Breakdown
Define the Cache Object
First, a cache object is defined to store the project codes. This helps in reducing the number of requests made to the data source.
let projectCodeCache = cache.getCache({
name: 'projectCodeCache'
});
Function to Fetch Project Codes from the Source
A helper function is defined to fetch the project codes from the original data source.
function fetchVendorListFromSource() {
let projectCodeList = dataModel.getResults.getProjectCodeListData();
return projectCodeList;
}
Retrieve Data from Cache or Source
The method attempts to retrieve the project code list from the cache. If not available, it fetches fresh data from the source and stores it in the cache.
let projectCodeList = projectCodeCache.get({
key: "projectCode",
loader: function () {
let freshProjectCodeList = fetchVendorListFromSource();
if (freshProjectCodeList.length > 0) {
projectCodeCache.put({
key: "projectCode",
value: freshProjectCodeList // Storing the array directly
});
}
return freshProjectCodeList;
}
});
Return the Project Code List
Finally, the method returns the project code list, handling any errors that may occur during the process.
if (projectCodeList && projectCodeList.__fromSource__) {
delete projectCodeList.__fromSource__;
}
return { status: "SUCCESS", reason: "PROJECT_CODES_FOUND", data: { projectCode: JSON.parse(projectCodeList) }};
} catch (e) {
log.error("Error @apiMethods @getProjectCodeList", e);
return { status: "ERROR", reason: "Error @getProjectCodeList", data: [] }
}
Retrieving and Grouping Project Codes by Department
The getProjectCodeListData method retrieves active project codes and groups them by department.
Code Breakdown
Define the Search Object
A search object is created to find active project codes from the custom record.
let projectCodeSearchObj = search.create({
type: "customrecord_cseg_jj_projectcode",
filters: [
["isinactive", "is", "F"]
],
columns: [
search.createColumn({ name: "internalid", label: "internal_id" }),
search.createColumn({ name: "name", label: "project_code" }),
search.createColumn({ name: "cseg_jj_projectcode_filterby_department", label: "department" }),
]
});
Iterate Through Search Results
The results of the search are iterated through, and a map is created to group project codes by department ID.
let resultObj = nsUtility.dataSets.iterateSavedSearch({
searchObj: projectCodeSearchObj,
columns: nsUtility.dataSets.fetchSavedSearchColumn(projectCodeSearchObj, "label"),
PAGE_INDEX: null,
PAGE_SIZE: 1000,
});
let departmentMap = {};
resultObj.forEach(result => {
let departmentIds = result.department.value.split(',');
let project = {
internal_id: result.internal_id.value,
project_code: result.project_code.value
};
departmentIds.forEach(departmentId => {
if (!departmentMap[departmentId]) {
departmentMap[departmentId] = [];
}
departmentMap[departmentId].push(project);
});
});
Return the Grouped Data
The final map, grouping project codes by department, is logged and returned.
log.debug("formattedResults", departmentMap);
return departmentMap;
} catch (e) {
log.error('Error @getResults @getProjectCodeListData', e);
return [];
}
Conclusion
By implementing these methods, you can efficiently retrieve and cache project codes, ensuring quick access and reduced load on the data source. Grouping project codes by department provides an organized structure, making it easier to manage and utilize the data.