Restricting Sales Order From Saving If It Is Trying To Connect With Already Connected RMA

The client needs to avoid the salesperson selecting the RMA when creating a sales order that is already connected with another Sales Order.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/search', 'N/ui/dialog'],
/**
 * @param{record} record
 * @param{search} search
 * @param{dialog} dialog
 * @param{serverWidget} serverWidget
 */
function(record, search, dialog) {
    
    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     */
    function pageInit(scriptContext) {

    }

    function relatedExchangeOrderDetails(rma){
        try{
            var returnauthorizationSearchObj = search.create({
                type: "returnauthorization",
                filters:
                    [
                        ["type","anyof","RtnAuth"],
                        "AND",
                        ["internalid","anyof",rma],
                        "AND",
                        ["mainline","is","T"],
                        "AND",
                        ["custbody_exchange_so_ahap_345","noneof","@NONE@"]
                    ],
                columns:
                    [
                        search.createColumn({
                            name: "tranid",
                            join: "CUSTBODY_EXCHANGE_SO_AHAP_345",
                            summary: "GROUP",
                            label: "Document Number"
                        }),
                        search.createColumn({
                            name: "statusref",
                            join: "CUSTBODY_EXCHANGE_SO_AHAP_345",
                            summary: "GROUP",
                            sort: search.Sort.ASC,
                            label: "Status"
                        }),
                        search.createColumn({
                            name: "formulatext",
                            summary: "GROUP",
                            formula: "{custbody_exchange_so_ahap_345.status}",
                            label: "Formula (Text)"
                        })
                    ]
            });
            var searchResultCount = returnauthorizationSearchObj.runPaged().count;
            console.log("searchResulCount",searchResultCount);
            var orderDetail=[]
            if(searchResultCount>0) {
                returnauthorizationSearchObj.run().each(function (result) {
                    var order = {}
                    order.orderNumber = result.getValue({
                        name: "tranid",
                        join: "CUSTBODY_EXCHANGE_SO_AHAP_345",
                        summary: "GROUP",
                        label: "Document Number"
                    })
                    order.status = result.getValue({
                        name: "statusref",
                        join: "CUSTBODY_EXCHANGE_SO_AHAP_345",
                        summary: "GROUP",
                        sort: search.Sort.ASC,
                        label: "Status"
                    })
                    order.textStatus = result.getValue({
                        name: "formulatext",
                        summary: "GROUP",
                        formula: "{custbody_exchange_so_ahap_345.status}",
                        label: "Formula (Text)"
                    })
                    orderDetail.push(order)

                });
                return orderDetail;
            }else {
                return []
            }
        }
        catch (e) {
            return []
            console.log("error@realtedExchangeOrderDetails",e)
        }
    }
    /**
     * Function to be executed when field is changed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
     * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
     *
     * @since 2015.2
     */
    function fieldChanged(scriptContext) {
        try{
            var currentRec = scriptContext.currentRecord
            if (scriptContext.fieldId == 'custbody29') {
                console.log("working in the field change");
                var selectedRMA=currentRec.getValue({
                    fieldId: 'custbody29'
                });
                if(selectedRMA){
                    var orderDetails=relatedExchangeOrderDetails(selectedRMA)
                    console.log("orderDetails",orderDetails)
                    console.log("length of the array",orderDetails.length)
                    if(orderDetails.length >0){
                        var orderNumber=orderDetails[0]['orderNumber']
                        console.log("orderNumber",orderNumber)
                        var orderStatus=orderDetails[0]['textStatus']
                        if((orderStatus!='Closed')|| orderDetails.status!='SalesOrd:H'){

                            alert("You Cannot Select the RMA, It is connected with an Exchange Sales Order"+'  '+orderNumber);
                            currentRec.setValue({
                                fieldId: 'custbody29',
                                value: ''
                            })
                        }
                    }
                }

            }
        }catch (e) {
            console.log("error@fieldChanged",e)
        }

    }

    /**
     * Function to be executed when field is slaved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     *
     * @since 2015.2
     */
    function postSourcing(scriptContext) {

    }

    /**
     * Function to be executed after sublist is inserted, removed, or edited.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     */
    function sublistChanged(scriptContext) {

    }

    /**
     * Function to be executed after line is selected.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     */
    function lineInit(scriptContext) {

    }

    /**
     * Validation function to be executed when field is changed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
     * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
     *
     * @returns {boolean} Return true if field is valid
     *
     * @since 2015.2
     */
    function validateField(scriptContext) {

    }

    /**
     * Validation function to be executed when sublist line is committed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     */
    function validateLine(scriptContext) {

    }

    /**
     * Validation function to be executed when sublist line is inserted.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     */
    function validateInsert(scriptContext) {

    }

    /**
     * Validation function to be executed when record is deleted.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     */
    function validateDelete(scriptContext) {

    }

    /**
     * Validation function to be executed when record is saved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @returns {boolean} Return true if record is valid
     *
     * @since 2015.2
     */
    function saveRecord(scriptContext) {

    }

    return {
        pageInit: pageInit,
        fieldChanged: fieldChanged,
        // postSourcing: postSourcing,
        // sublistChanged: sublistChanged,
        // lineInit: lineInit,
        // validateField: validateField,
        // validateLine: validateLine,
        // validateInsert: validateInsert,
        // validateDelete: validateDelete,
        //saveRecord: saveRecord
    };
    
});

Leave a comment

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