Restrict line-level field change based on Condition

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

/*Script Description: Show alert and block committing line on changing quantity in the line if the item on it is already received.
*/

define(
    [ 'N/currentRecord' ],

    function ( currentRecord )
    {
        var existingValueObj = {};

        function pageInit ( scriptContext )
        {
            try
            {
                if ( scriptContext.mode !== 'edit' )
                    return;
                var rec = scriptContext.currentRecord;
                var lineCount = rec.getLineCount( {
                    sublistId: 'item'
                } );
                console.log( "lineCount", lineCount )
                for ( var i = 0; i < lineCount; i++ )
                {
                    var qtyOld = rec.getSublistValue( {
                        sublistId: 'item',
                        fieldId: 'quantity',
                        line: i
                    } );
                    var lineOld = rec.getSublistValue( {
                        sublistId: 'item',
                        fieldId: 'line',
                        line: i
                    } );
                    existingValueObj[ lineOld ] = qtyOld

                }
                console.log( "existingValueObj", existingValueObj )
            } catch ( e )
            {
                log.debug( "error@pageInit", e.message )
            }
        }

        function validateLine ( scriptContext )
        {
            try
            {
                console.log( "Entered validateLine" )
                if ( scriptContext.sublistId === "item" || scriptContext.fieldId === "quantity" )
                {

                    var rec = scriptContext.currentRecord;
                    var lineNew = rec.getCurrentSublistValue( {
                        sublistId: 'item',
                        fieldId: 'line'
                    } );
                    console.log( "lineNew", lineNew )

                    var qtynew = rec.getCurrentSublistValue( {
                        sublistId: 'item',
                        fieldId: 'quantity'
                    } );
                    console.log( "qtynew", qtynew )

                    var qtyRecieved = rec.getCurrentSublistValue( {
                        sublistId: 'item',
                        fieldId: 'quantityreceived'
                    } );
                    if ( qtyRecieved > 0 )
                    {
                        console.log( "qtyRecieved", qtyRecieved )
                        if ( qtynew != existingValueObj[ lineNew ] )
                        {
                            console.log( "test", existingValueObj[ lineNew ] )
                            alert( "Items on this line have been received. You cannot modify the quantity" )
                            return false;
                        }
                        else
                        {
                            console.log( "Entered Here" );
                            return true;
                        }
                    }
                    else
                    {
                        return true;
                    }
                }

            } catch ( e )
            {
                log.debug( "error@validateLine", e.message )
            }
        }

        return {
            pageInit: pageInit,
            validateLine: validateLine
        };

    } );

Leave a comment

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