Implement End of Month Sales Order Promotions

This sample is a user event script that implements end of month sales order promotions.

The conversion of this script from SuiteScript 1.0 to SuiteScript 2.1 includes the following:

SuiteScript 1.0 Script

SuiteScript 2.1 Script

                    function customizeUI_SalesOrderBeforeLoad(type, form) {
  var currentContext = nlapiGetContext();
   
  if (type == 'create' && currentContext.getExecutionContext() == 'userinterface')
  {
    var fieldId = 'custpage_eom_promotion';
    var fieldLabel = 'Eligible EOM promotion';
    var today = new Date();
    var month = today.getMonth();
    var date = today.getDate();

    nlapiLogExecution('DEBUG', 'month date', month + ' ' + date);
      
    //February
    if (month==1)
    {
      if (date==24 | date==25 | date==26 | date==27 | date==28 | date==29)
      form.addField(fieldId, 'checkbox', fieldLabel);
    }
    //31-day months
    else if (month==0 | month==2 | month ==4 | month==6 | month==7 | month==9 | month==11)
    {
      if ( date==27 | date==28 | date==29 | date==30 | date==31)
      form.addField(fieldId, 'checkbox', fieldLabel);
    }
    //30-day months
    else
    {
      if ( date==26 | date==27 | date==28 | date==29 | date==30)
      form.addField(fieldId, 'checkbox', fieldLabel);
    }
  }   
} 

                  
                    /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

define(['N/ui/serverWidget'], (serverWidget) => {
    const beforeLoad = (context) => {
        if (context.type !== context.UserEventType.CREATE) {
            return;
        }

        const today = new Date();
        const lastDayOfMonth = new Date(
            today.getFullYear(),
            today.getMonth() + 1,
            0
        ).getDate();

        const daysRemainingInMonth = lastDayOfMonth - today.getDate();

        log.debug({
            title: 'End-of-Month Promotion Check',
            details: `Today is ${today.getMonth() + 1}/${today.getDate()}. Days remaining: ${daysRemainingInMonth}`
        });

        // Display the promotion checkbox during the last five days of the month.
        if (daysRemainingInMonth <= 4) {
            context.form.addField({
                id: 'custpage_eom_promotion',
                label: 'Eligible EOM promotion',
                type: serverWidget.FieldType.CHECKBOX
            });
        }
    };

    return {
        beforeLoad
    };
}); 

                  

General Notices