/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record'],
(record) => {
const FOLDEROBJ = {
"customer" :1160,
"vendor" :1161,
"inventoryitem" :1164,
"lotnumberedinventoryitem" :1176,
"noninventoryitem" :1170,
"serializedassemblyitem" :1174,
"serializedinventoryitem" :1178,
"serviceitem" :1172,
"expensereport" :1162
}
/**
* Function definition to be triggered 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
* @since 2015.2
*/
const afterSubmit = (scriptContext) => {
try{
if (scriptContext.type === scriptContext.UserEventType.CREATE) {
// get the record type
let recordType = scriptContext.newRecord.type;
log.debug('recordType',recordType);
// get the record id
let recordId = scriptContext.newRecord.id;
// get the record folder name is 'Record'+ record internal id
let folderName = "Record " +recordId;
// create a folder in the file cabinet
let folderId = createFolder(recordType,folderName);
}
}
catch(e){
log.error('Error@afterSubmit',e);
}
}
/**
* Function definition to create a folder in the File Cabinet.
* @param {string} folderName - Name of the folder
* @returns {number} - Internal ID of the folder
*/
const createFolder = (recordType,folderName) => {
try{
log.debug('recordType',recordType);
log.debug('folderName',folderName);
// create a folder in the file cabinet
let folderRecord = record.create({
type: record.Type.FOLDER,
isDynamic: true
});
log.debug('folderRecord',folderRecord);
//save folder in another folder
folderRecord.setValue('name', folderName);
folderRecord.setValue('parent', FOLDEROBJ[recordType]);
return folderRecord.save();
}
catch(e){
log.error('Error@createFolder',e);
}
}
return {afterSubmit}
});