Remove Backordered Item Quantity from Transfer Order

Requirement:

In transfer order, if an item line is picked, then its quantity cannot be changed.  But here the client wants to change this quantity field for back ordered items and wants to remove the back ordered quantity.  

For example, if the quantity of an item is 10, but only 8 are available, so there will be 2 quantity back ordered. If at least 1 quantity of this item line is picked, then we cannot change the quantity. But client wants to change the quantity to 8 (quantity committed)   

For this, we have created a new column named Custom Quantity in the item sub list and applied the custom field to transfer order records.  

User can enter the quantity to be updated in the Quantity line field into this custom field. If 8 is entered in this custom field, then 8 will be updated automatically in the Quantity field only if quantity back ordered and quantity picked in the item line are greater than 0.  

If the item is not picked, we do not need to have this custom functionality work as we can ourselves change the quantity field.  

Solution:

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/***********************************************************************************************
 *
 * Author: Jobin & Jismi IT Services LLP
 * Client Name:Corp Design US(CDU)
 * Date Created : 11-February-2023
 * Created By: JJ0152 Jobin & Jismi IT Services LLP
 * Script Description : This script is to remove the back ordered item quantity from TO using a
 * custom quantity field, if the item line is picked.
 *
 ***********************************************************************************************/
define(['N/currentRecord', 'N/record'],
/**
 * @param{currentRecord} currentRecord
 * @param{record} record
 */
function(currentRecord, record) {
    

    function fieldChanged(scriptContext) {
        try {
            var currentRecord = scriptContext.currentRecord;
            if (scriptContext.sublistId == 'item' && scriptContext.fieldId == 'custcol_jj_customquantity' ) {
                var status = currentRecord.getValue({
                    fieldId: 'status'
                })
                console.log('status', status);
                if (status == 'Pending Fulfillment' || 'Partially Fulfilled' || 'Pending Receipt/Partially Fulfilled'){

                    var qtyBackOrd = currentRecord.getCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'quantitybackordered'
                    });
                var qtyPicked = currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantitypicked'
                });
                var qtyOrdered = currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity'
                });
                var qtyCommitted = currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantitycommitted'
                });
                var qtyShipped = currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantityfulfilled' || 0
                });

                var newQty = currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_customquantity'
                });
                console.log('newQty', newQty);

                //continue execution only if the line is back ordered and picked quantity greater than 0
                if (qtyPicked > 0 && qtyBackOrd > 0 && newQty > 0) {
                    
                    if (newQty < qtyPicked) {
                        alert("Items on this line have been picked, you cannot enter a value which is less than the picked quantity.")
                        currentRecord.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'custcol_jj_customquantity',
                            value: ""
                        });
                    } else if (newQty > qtyOrdered) {
                        var message = confirm("You have only " + qtyCommitted + " available for commitment at this location. You cannot enter a value greater than the current ordered quantity")
                        currentRecord.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'custcol_jj_customquantity',
                            value: ""
                        });
                    } else if (newQty < (qtyCommitted + qtyShipped)) {
                    var message = confirm("You can only remove the back ordered item quantity from this item line")
                    currentRecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_jj_customquantity',
                        value: ""
                    });
                }
                    else {
                        currentRecord.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            value: newQty
                        });
                    }
                } else {
                    currentRecord.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_jj_customquantity',
                        value: ""
                    });
                }
            }
            }
        }catch (e) {
            log.debug("error @ field changed", e.message);
        }
    }

    return {
        fieldChanged: fieldChanged
    };
    
});

Leave a comment

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