USER EVENT SCRIPT SAMPLE TO RESTRICT THE CREATION OF A CUSTOM RECORD IF DUPLICATE RECORD IS FOUND

This user event script sample restricts the reaction of a custom record if duplicate records are found.

 const beforeSubmit = (scriptContext) => {

            let isErrorDuplicate = false;

            let isErrorTerminated = false;

            let duplicateErrorMsg = 'A payroll record already exists for the selected month and year.';

            let terminationErrorMsg = 'The employee has been terminated before the selected month and year.';




            try {




                if ((scriptContext.type === scriptContext.UserEventType.CREATE) && (scriptContext.type === scriptContext.UserEventType.EDIT)) return; // Only validate on create and edit




                let newRecord = scriptContext.newRecord;

                let employeeId = newRecord.getValue('custrecord_jj_payroll_emp');

                log.debug('employeeId', employeeId);

                let payrollMonth = newRecord.getValue('custrecord_jj_month');

                log.debug('payrollMonth', payrollMonth);

                let payrollYear = newRecord.getValue('custrecord_jj_year');

                log.debug('payrollYear', payrollYear);

                let empTerminationDateLookUp = search.lookupFields({

                    type: 'employee',

                    id: employeeId,

                    columns: ['releasedate']

                });

                log.debug('empTerminationDateLookUp', empTerminationDateLookUp);

                let empTerminationDate = empTerminationDateLookUp.releasedate || '';

                log.debug('empTerminationDate', empTerminationDate);

                // Call the new function to check the termination date

                isErrorTerminated = checkEmployeeTermination(empTerminationDate, payrollMonth, payrollYear);

                // Call the new function to check for existing records  

                isErrorDuplicate = checkExistingPayrollRecord(employeeId, payrollMonth, payrollYear);

            }

            catch (e) {

                log.error('error@beforeSubmit', e)




            }

            if (isErrorDuplicate === true) {

                throw duplicateErrorMsg

            } else if (isErrorTerminated === true) {

                throw terminationErrorMsg

            }




        }


 function checkEmployeeTermination(empTerminationDate, payrollMonth, payrollYear) {
            try {
                let isError = false;
                if (empTerminationDate) {
                    log.debug('empTerminationDate', empTerminationDate);


                    // Split termination date and handle invalid formats
                    let empTerminationDateSplit = empTerminationDate.split('/');
                    let empTerminationMonth = parseInt(empTerminationDateSplit[1], 10);  // Month
                    let empTerminationYear = parseInt(empTerminationDateSplit[2], 10);   // Year


                    log.debug('empTerminationDateMonth', empTerminationMonth);
                    log.debug('empTerminationDateYear', empTerminationYear);


                    // Ensure proper comparison for terminated employees
                    if (
                        (Number(empTerminationYear) < Number(payrollYear)) || // Termination year is before payroll year
                        (empTerminationYear === payrollYear && Number(empTerminationMonth) < Number(payrollMonth)) // Termination month is before payroll month
                    ) {
                        isError = true;
                    }
                    return isError;
                }
            } catch (e) {
                log.error('error@checkEmployeeTermination', e);
                return false
            }
        };
        function checkExistingPayrollRecord(employeeId, payrollMonth, payrollYear) {
            try {
                let isError = false;
                log.debug("obj", { employeeId, payrollMonth, payrollYear })
                // Create a search for existing Payroll Details
                let payrollSearch = search.create({
                    type: 'customrecord_jj_payroll_details',
                    filters: [
                        ["custrecord_jj_payroll_emp", "anyof", employeeId],
                        "AND",
                        ["custrecord_jj_month", "anyof", payrollMonth],
                        "AND",
                        ["custrecord_jj_year", "equalto", payrollYear]
                    ],
                    columns: [
                        search.createColumn({ name: "id", label: "ID" }),
                    ]
                });


                let existingRecord = false;
                let searchResultCount = payrollSearch.runPaged().count;
                log.debug('searchResultCount', searchResultCount);
                // Run the search and check if any record exists
                if (searchResultCount > 0) {
                    existingRecord = true;
                    log.debug('Existing Payroll Record Found');
                    return false; // Exit loop after finding one record
                }


                // If an existing record is found, throw an error
                if (existingRecord) {
                    isError = true;
                }
                return isError;
            } catch (e) {
                log.error('error@checkExistingPayrollRecord', e);
                return false
            }
        };

Leave a comment

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