Handling Custom Records to schedule a NetSuite script

Following is a script logic I wrote to schedule a suite script based on custom record fields similar to the schedule tab in NetSuite workflow:

// Get current date and time
                let currentDate = new Date();
        
                // Adjust the time to NetSuite's timezone
                let formattedDate = format.format({
                    value: currentDate,
                    type: format.Type.DATETIME
                });
        
                // Parse the formatted date to get the correct hour and minute
                let parsedDate = new Date(formattedDate);
                let executedMinute = parsedDate.getMinutes();
                let executedHour = parsedDate.getHours();
                let timeRangeHHMM = executedMinute < 30 ? `${executedHour}:00` : `${executedHour}:30`;
                log.debug('Values', {'timeRangeHHMM': timeRangeHHMM, 'executedMinute': executedMinute, 'executedHour': executedHour, 'frequency': frequency, 'repeatChecked': repeatChecked});


                // Parse the dates
                let parsedFromDate = new Date(scheduleFromDate);
                let parsedToDate = new Date(scheduleToDate);


                // Set fromDate to the first second of the day
                parsedFromDate.setHours(0, 0, 0, 0);


                // Set toDate to the last second of the day
                parsedToDate.setHours(23, 59, 59, 999);
                // Check if the frequency is null (single execution)
                if (!repeatChecked) {
                    if (executionDate && new Date(executionDate).toDateString() === parsedDate.toDateString()) {
                        scheduleTheScript();
                    }
                } else {
                    // Check if the current date falls under the schedule date range
                    
                    if (parsedFromDate && parsedToDate && parsedDate >= parsedFromDate && parsedDate <= parsedToDate) {
                        // Check if the current time matches the execution start time
                        if (executionStartTime && executionStartTime === timeRangeHHMM) {
                            if (frequency === '1') { // Daily
                                if (repeatEveryWeekDay) {
                                    scheduleTheScript();
                                }
                                if (repeatAfterNDays) {
                                    if(isSameDay(parsedFromDate, parsedDate)){ // Start date and current date are the same
                                        scheduleTheScript();
                                    } else{ // Repeat logic
                                        let daysBetweenDates = getDaysBetweenDates(parsedFromDate, parsedDate);repeatAfterNDaysValue });
                                        (daysBetweenDates % repeatAfterNDaysValue === 0) ? scheduleTheScript() : null;
                                    }
                                }
                            } else if (frequency === '2') { // Weekly
                                const dayFieldMap = {
                                    0: 'custrecord_pdg_dp_repeat_on_sunday',
                                    1: 'custrecord_pdg_dp_repeat_on_monday',
                                    2: 'custrecord_pdg_dp_repeat_on_tuesday',
                                    3: 'custrecord_pdg_dp_repeat_on_wednesday',
                                    4: 'custrecord_pdg_dp_repeat_on_thursday',
                                    5: 'custrecord_pdg_dp_repeat_on_friday',
                                    6: 'custrecord_pdg_dp_repeat_on_saturday'
                                };
                                const currentDayField = dayFieldMap[parsedDate.getDay()];
                                if(recObj.values[currentDayField]){ // Check if the current day is selected for dunning
                                    if(isSameDay(parsedFromDate, parsedDate)){  // Start date and current date are the same
                                        scheduleTheScript()
                                    } else{ // Repeat logic
                                        let weeksBetweenDates = getWeeksBetweenDates(parsedFromDate, parsedDate);
                                        (weeksBetweenDates % repeatAfterXWeeks === 0) ? createDunningTask(scheduledDunningProcedureId) : null;
                                    }
                                }
                            } else if (frequency === '3') { // Monthly
                                if (repeatMonthWeekday) {
                                    if (recObj.values['custrecord_pdg_dp_repeat_on_week_month'] && recObj.values['custrecord_pdg_dp_repeat_weekday_month']) {
                                        if (recObj.values['custrecord_pdg_dp_repeat_on_week_month'].length > 0 && recObj.values['custrecord_pdg_dp_repeat_weekday_month'].length > 0) {
                                            let weekOfMonth = parseInt(recObj.values['custrecord_pdg_dp_repeat_on_week_month'][0].value);
                                            let weekDayOfMonth = parseInt(recObj.values['custrecord_pdg_dp_repeat_weekday_month'][0].value);
                                            let dateCheck = checkWeekIsMatching(parsedDate, weekOfMonth, weekDayOfMonth);
                                            if (dateCheck) {
                                                let monthRepeatCount = recObj.values['custrecord_pdg_dp_repeat_on_n_months'];
                                                if (monthRepeatCount === '') {
                                                    scheduleTheScript()
                                                } else {
                                                    let monthDifference = parsedDate.getMonth() - parsedFromDate.getMonth();
                                                    (monthDifference % monthRepeatCount === 0) ? scheduleTheScript() : null;
                                                }
                                            }
                                        }
                                    }
                                } else if (repeatOnMonthDay) {
                                    // Add logic for repeatOnMonthDay
                                    let currentDayOfMonth = parsedDate.getDate();
                                    let selectedDayOfMonth = parseInt(recObj.values['custrecord_pdg_dp_repeat_on_day_of_month']);
                                    
                                    if (currentDayOfMonth === selectedDayOfMonth) {
                                        let monthRepeatCount = recObj.values['custrecord_pdg_dp_repeat_on_n_months'];
                                        if (monthRepeatCount === '') {
                                            scheduleTheScript()
                                        } else {
                                            let monthDifference = parsedDate.getMonth() - parsedFromDate.getMonth();
                                            (monthDifference % monthRepeatCount === 0) ? scheduleTheScript() : null;
                                        }
                                    }
                                } else {
                                    log.debug("Missing fields for monthly frequency");
                                }
                            }
                        }

Leave a comment

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