Update Due date based on Uploading Date in the Invoice Record

Requirement 

The client requirement is to update the due date in the invoice record according to the uploading date and the customer’s terms of billing.

While doing a CSV import, the  “uploading date” should be populated through the import and the script will be updating the due date(field id: “duedate”) accordingly

  • While uploading the file, the user will map the custom field ‘Uploading Date’
  • User will enter the customer terms field value through UI or CSV import

Solution

Here we are using a custom field “UPLOADING DATE” and a standard field “TERM” to calculate the “DUE DATE” in the Invoice record. This script will work when we create or edit a Invoice record through Netsuite UI/ CSV Import.

The script will update the “DUE DATE” if the value selected in both the fields “UPLOADING DATE” and “TERMS” . Otherwise we will consider the Transaction Date and the corresponding Term for calculating the “DUE DATE” .

Here we are using the User Event Script with Entry Point “Before Submit”

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*******************************************************************************
 * CLIENTNAME:AL-GARAWI GROUP
 * ALGAR-101
 * Updating due date based on uploading date
 *************************************************************************
 * Date : 08-12-2021
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description :
 * The script is used to update due date based on the uploading date and the payment terms in the invoice record
 * Date created :08-12-2021
 * Created by: Aranya T R, Jobin & Jismi IT Services LLP
 * REVISION HISTORY
 * Revision 1.0 ${08-12-2021}
 *
 ******************************************************************************/

define(['N/record', 'N/search'], (record, search) =>
{
    //Function to lookup fields from term record
    function Lookup(terms)
    {
        return search.lookupFields({
            type : "term",
            id : terms,
            columns : ["daysuntilnetdue"]
        });
    }
    const beforeSubmit = (scriptContext) =>
    {
        try
        {
            if (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.UserEventType.EDIT)
            {
                let invoiceRecord = scriptContext.newRecord;
                //To get Uploading date
                let uploadingDate = invoiceRecord.getValue({fieldId: 'custbody_jj_uploading_date'});
                //To get selected terms
                let terms = invoiceRecord.getValue({fieldId: 'terms'});
                //Calling Lookup function if term has a value
                let number_of_days;
                if(terms)
                    number_of_days = Lookup(terms).daysuntilnetdue;
                let trandate ;
                //Set due date as Uploading date + Number of days if condition satisfies
                if(uploadingDate && terms)
                {
                    uploadingDate.setDate( Number (uploadingDate.getDate()) + Number( number_of_days ));
                    invoiceRecord.setValue({ fieldId : "duedate",
                        value  : uploadingDate });
                }
                else
                {
                    //get transaction date
                    trandate = invoiceRecord.getValue({ fieldId: 'trandate'});
                    //if term field is not empty
                    if(terms)
                    {
                        trandate.setDate( Number (trandate.getDate()) + Number( number_of_days ));
                        invoiceRecord.setValue({ fieldId : "duedate",
                            value   : trandate });
                    }
                    //if term is empty set duedate as trandate
                    else
                        invoiceRecord.setValue({ fieldId : "duedate",
                            value   : trandate });
                }
            }
        }
        catch(e)
        {
            log.debug("Error", e);
        }
    }
    return { beforeSubmit}
});

Leave a comment

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