PVB – Parked Vendor Bill List – update of status

Description

We have a custom record named Parked Vendor Bill and need to perform the following changes to the account.

We will add a new script to make the comparison between item receipt and vendor bill in the parked vendor bill. If based on the comparison a particular item receipt is fully billed, we will change the status to fully billed regardless of the status of purchase order.

If a parked vendor bill has more than one purchase order, the status will be set to fully billed only if all the item receipts (in this PVB) are fully billed.

Solution

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/****************************************************************************
 * World Remi |WR-580 | PVB - Parked Vendor Bill List - update of status
 * **************************************************************************
 * Date: 22-03-2022
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 * Description:
 * Update status of PVB based on PO, IR and Bill
 *
 * Revision  History:
 * Revision 1.0 ${22-03-2022} Aranya T R : created
 *
 *
 ******************************************************************************/
define(['N/record', 'N/search'], (record, search) =>
    {
        const beforeLoad = (scriptContext) =>
        {
            try
            {
                if( scriptContext.type === 'create' || 'view' || 'edit' )
                {
                    let PVBrec = scriptContext.newRecord;
                    //Getting purchase order
                    let poRec = PVBrec.getValue({ fieldId : 'custrecord_jj_purchase_order_wr_218' });
                    log.debug( "Purchase orders -PVB", poRec );
                    log.debug("PO length", poRec.length );
                    //Getting the Bill
                    let billRec = PVBrec.getValue({ fieldId : 'custrecord_jj_vendor_bill_wr_218' });
                    log.debug( "Bill ID -PVB", billRec );
                    let item_id_po = [];
                    let item_id_bill = [];
                    let item_id_from_itemline ={};


                    if ( (poRec.length > 0) && (billRec.length > 0) )
                    {
                        for (let i = 0; i < poRec.length; i++)
                        {
                            log.debug( "purchase order" + (i + 1), poRec[i] );
                            //Loading purchase order
                            let purchase_rec = record.load({
                                type: record.Type.PURCHASE_ORDER,
                                id: poRec[i],
                                isDynamic: true
                            });

                            //get item line count of particular purchase order
                            let line_count = purchase_rec.getLineCount({
                                sublistId: 'item'
                            });
                            log.debug('line_count of PO', line_count);

                            // Looping through each item line in Purchase Order
                            for (let j = 0; j < line_count; j++)
                            {
                                purchase_rec.selectLine({
                                    sublistId: 'item',
                                    line: j
                                });
                                //id of item from item line
                                item_id_from_itemline = purchase_rec.getCurrentSublistValue({
                                        sublistId: 'item',
                                        fieldId: 'item'
                                });
                                item_id_po.push(item_id_from_itemline);
                            }
                        }
                        log.debug('Item id from PO', item_id_po);

                        //Looping through Bill record
                        for ( let i = 0; i < billRec.length; i++ )
                        {
                            log.debug( "Bill" + (i + 1), billRec[i] );
                            //Loading purchase order
                            let bill_rec = record.load({
                                type: record.Type.VENDOR_BILL,
                                id: billRec[i],
                                isDynamic: true
                            });

                            //get item line count of particular bill
                            let line_count = bill_rec.getLineCount({
                                sublistId: 'item'
                            });
                            log.debug('line_count of bill', line_count);

                            // Looping through each item line in Purchase Order
                            for (let j = 0; j < line_count; j++)
                            {
                                bill_rec.selectLine({
                                    sublistId: 'item',
                                    line: j
                                });
                                //id of item from item line
                                item_id_from_itemline = bill_rec.getCurrentSublistValue({
                                    sublistId: 'item',
                                    fieldId: 'item'
                                });
                                item_id_bill.push(item_id_from_itemline);
                            }
                        }
                        log.debug('Item id from Bill', item_id_bill);
                    } // if po && bills


                    //Checking PO_items is in bills_items
                    if ( ( poRec.length > 1 ) && billRec.length > 0 && ( item_id_po == item_id_bill ) )
                    {
                        log.debug ( "Inside IF");
                        //Setting status of PVB as Fully Billed if all items in PO  are billed
                        record.submitFields({
                                id     : PVBrec.id,
                                type   : 'customrecord_jj_proforma_bill_wr_218',
                                values : { 'custrecord_jj_status_wr_218': 6 }
                        });
                    }
                    else if ( poRec.length === 1 && billRec.length > 0 )
                    {
                        log.debug("Inside ELSE");
                        for ( let i = 0; i <= item_id_po.length; i++ )
                        {
                            for ( let j= 0; j <= item_id_bill.length; j++ )
                            {
                                if (  item_id_po[i] == item_id_bill[j]  )
                                {
                                    log.debug("po[i] equals bills[j]");
                                    //Setting status as Fully Billed when one item in PO is billed
                                    record.submitFields({
                                        id     : PVBrec.id,
                                        type   : 'customrecord_jj_proforma_bill_wr_218',
                                        values : { 'custrecord_jj_status_wr_218': 6 }
                                    });
                                }
                            }
                        }
                    }
                } //context type
            } //try

            catch(e)
            {
                log.debug( "Error at Loading", e.message);
            }
        }
        return { beforeLoad }

    });

Leave a comment

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