How to perform Purchase Order Validation using CSV Import

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/**
 * * *************************************************************************************************
 * 
 * Joinbranded-France-NS
 * 
 * JOINF-1735 : Validation on PO Approval Workflow - Scripting possibility 
 * 
 * *****************************************************************************************************
 * 
 * Author : Jobin and Jismi IT Services
 * 
 * Date Created: 08 - June - 2023
 * 
 * Created by: Linu Paul, Jobin and Jismi IT Services
 * 
 * Description: In this User Event Script, we checked all the PO validations. 
  
 * Revision History: 
 * 
 * *
 * ********************************************************************************************************
 */
define(['N/error', 'N/record'],
    /**
 * @param{error} error
 * @param{record} record
 */
    (error, record) => {

        /**
         * 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
         */
        const beforeSubmit = (scriptContext) => {


            let newRec = scriptContext.newRecord;
            let createdDate = newRec.getValue({
                fieldId: "trandate"
            });
            let curDate = new Date("6/8/2023");
            let customError;
            if (newRec.type === "purchaseorder") {

                if (createdDate >= curDate) {
                    if (scriptContext.type === "create" || scriptContext.type === "edit") {

                        let lineCount = newRec.getLineCount({
                            sublistId: "item"
                        });
                        for (let i = 0; i < lineCount; i++) {
                            let itemDisplayType = newRec.getSublistValue({
                                sublistId: "item",
                                fieldId: "itemtype",
                                line: i
                            });
                            log.debug("item type", itemDisplayType);
                            if (itemDisplayType == "InvtPart" || itemDisplayType == "Assembly" || itemDisplayType == "Kit") {
                                let itemLoc = newRec.getValue({
                                    fieldId: "location"
                                });
                                log.debug("location", itemLoc);
                                if (itemLoc == "") {
                                    i = i + 1;
                                    customError = error.create({
                                        name: 'ALERT MESSAGE',
                                        message: "In line " + i + " Location at body level should not be empty.",
                                        notifyOff: false
                                    });
                                    throw customError.message;
                                }
                                let itemType = newRec.getSublistValue({
                                    sublistId: "item",
                                    fieldId: "custcol_jj_item_type",
                                    line: i
                                });
                                log.debug("product type", itemType);
                                if (itemType == 1) {
                                    let landedCost = newRec.getSublistValue({
                                        sublistId: "item",
                                        fieldId: "custcol_jj_lc_template_peritem",
                                        line: i
                                    });
                                    if (landedCost == "") {
                                        i = i + 1;
                                        customError = error.create({
                                            name: 'ALERT MESSAGE',
                                            message: "In line " + i + "Please ensure that the Landed Cost field is not empty when the Item type is set to Product. To do this, select the appropriate Item Alias search PO or select a value from Landed cost template per item.",
                                            notifyOff: false
                                        });
                                        throw customError.message;

                                    }
                                }
                                let itemAliasSku = newRec.getSublistValue({
                                    sublistId: "item",
                                    fieldId: "custcol_jj_item_alias",
                                    line: i

                                });
                                if (itemAliasSku == "") {
                                    i = i + 1
                                    customError = error.create({
                                        name: 'ALERT MESSAGE',
                                        message: "In line " + i + " Please ensure that the Item Alias SKU is filled in for inventory, assembly, or kit items. Please select an Item Alias search PO.",
                                        notifyOff: false,

                                    });
                                    throw customError.message;

                                }
                                let itemAliasSkuSearch = newRec.getSublistValue({
                                    sublistId: "item",
                                    fieldId: "custcol_jj_combined_item_alias",
                                    line: i

                                });
                                if (itemAliasSkuSearch == "") {
                                    i = i + 1;
                                    customError = error.create({
                                        name: 'ALERT MESSAGE',
                                        message: "In line " + i + "Item Alias search PO should always be populated.",
                                        notifyOff: false
                                    });
                                    throw customError.message;

                                }
                                let itemGeography = newRec.getSublistValue({
                                    sublistId: "item",
                                    fieldId: "cseg_geography",
                                    line: i
                                });
                                log.debug("geograpgy", itemGeography);
                                let itemLocation = newRec.getSublistValue({
                                    sublistId: "item",
                                    fieldId: "location",
                                    line: i
                                });
                                if (itemGeography == "<Type then tab>" || itemGeography == "") {
                                    i = i + 1
                                    customError = error.create({

                                        name: 'ALERT MESSAGE',
                                        message: "In line " + i + " Geography field should not be empty.",
                                        notifyOff: false
                                    });
                                    throw customError.message;

                                }
                                if (itemLocation == "<Type then tab>" || itemLocation == "") {
                                    i = i + 1;
                                    customError = error.create({
                                        name: 'ALERT MESSAGE',
                                        message: "In line " + i + " Line Location field should not be empty.",
                                        notifyOff: false
                                    });
                                    throw customError.message;

                                }
                                if (itemLoc != itemLocation) {
                                    i = i + 1;
                                    customError = error.create({
                                        name: 'ALERT MESSAGE',
                                        message: "In line " + i + " Location at both body level and line level should be same.",
                                        notifyOff: false
                                    });
                                    throw customError.message;

                                }
                            }
                        }
                    }
                }
            }

        }
        return { beforeSubmit }

    });

Leave a comment

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