Create a custom Record and add the custom Field

Add the Script as shown below
define([‘N/record’, ‘N/search’, ‘N/log’],
/**
* @param{action} action
*/
(record, search, log) => {
/**
* Defines the function definition that is executed before 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
*/
function beforeSubmit(context) {
if (context.type == context.UserEventType.CREATE || context.type == context.UserEventType.COPY){
try {
let newRecord = context.newRecord;
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
// Determine the fiscal year start
let fiscalYearStart = (month >= 4) ? year : year – 1;
let fiscalYearShort = fiscalYearStart.toString().substring(2);
// Load the custom record to get the current sequence number
let sequenceRecordId = getSequenceRecordId(fiscalYearShort);
let sequenceRecord = record.load({
type: ‘customrecord_jj_project_sequence_traker’,
id: sequenceRecordId
});
// Reset the sequence if today is April 1st or this is the first project record of the fiscal year
if (isAprilFirst(date) || isFirstProjectOfYear(fiscalYearShort)) {
sequenceRecord.setValue(‘custrecord_jj_current_sequence_number’, 0);
}
let codeIdentifier=sequenceRecord.getValue(‘custrecord_jj_code_identifier’);
let currentSequence = sequenceRecord.getValue(‘custrecord_jj_current_sequence_number’);
currentSequence++;
// Update the custom record with the new sequence number
sequenceRecord.setValue(‘custrecord_jj_current_sequence_number’, currentSequence);
sequenceRecord.save();
// Generate the Project ID
let projectId = fiscalYearShort + (codeIdentifier + currentSequence).slice(–3);
newRecord.setValue(‘entityid’, projectId);
} catch (e) {
log.error(‘Error generating Project ID’, e.toString());
}
}
}
/**
* Gets the sequence record ID for the given fiscal year.
*
* @param {string} fiscalYearShort – The short representation of the fiscal year.
* @returns {number} The internal ID of the sequence record.
*/
function getSequenceRecordId(fiscalYearShort) {
try {
let sequenceSearch = search.create({
type: ‘customrecord_jj_project_sequence_traker’,
filters: [
[“custrecord_jj_project_sequence_name”,“is”,fiscalYearShort]
],
columns: [‘internalid’]
});
let results = sequenceSearch.run().getRange({
start: 0,
end: 1
});
if (results.length > 0) {
return results[0].getValue(‘internalid’);
}else {
// Create a new record if it does not exist
let sequenceRecord = record.create({
type: ‘customrecord_jj_project_sequence_traker’
});
sequenceRecord.setValue(‘custrecord_jj_project_sequence_name’, fiscalYearShort);
sequenceRecord.setValue(‘custrecord_jj_current_sequence_number’, 0);
return sequenceRecord.save();
}
} catch (e) {
log.error(‘Error getting sequence record ID’, e.toString());
throw e;
}
}
/**
* Checks if the given date is April 1st.
*
* @param {Date} date – The date to check.
* @returns {boolean} True if the date is April 1st, false otherwise.
*/
function isAprilFirst(date) {
return date.getDate() === 1 && date.getMonth() === 3; // JavaScript months are 0-based (0 = January, 3 = April)
}
/**
* Checks if the current record is the first project of the fiscal year.
*
* @param {string} fiscalYearShort – The short representation of the fiscal year.
* @returns {boolean} True if this is the first project of the fiscal year, false otherwise.
*/
function isFirstProjectOfYear(fiscalYearShort) {
try {
let projectSearch = search.create({
type: ‘job’, // ‘job’ is the internal ID for projects
filters: [
[‘entityid’, ‘startswith’, fiscalYearShort]
],
columns: [‘internalid’]
});
let results = projectSearch.run().getRange({
start: 0,
end: 1
});
return results.length === 0;
} catch (e) {
log.error(‘Error checking first project of the year’, e.toString());
throw e;
}
}
return {
beforeSubmit: beforeSubmit,
};
});