Adjust the Sales order to match with Invoice

Jira Code: BTN-601

The purpose of the task is to adjust the Sales order to match the INVOICE. Written a scheduled script to match the quantity to quantity billed and change the status of partially fullfilled to billed.

Scheduled Script

/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
/**
 * Script Description
 *  Field updation script - Scheduled script to match the quantity to quantity billed and change the status 
    of partially fullfilled to billed
 */
/*******************************************************************************
 * BALAJI WIRELESS
 * **************************************************************************
 * 
 * Date: 28/01/2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY
 * 
 * Revision 1 $ 29/01/2019 Maria 
 * 
 ******************************************************************************/

define(['N/record', 'N/search'],

    function(record, search) {


        function execute(scriptContext) {

            try {

                var salesorderSearchObj = search.load({
                id: 'customsearch12104'
                });
                logme("ScheduledScript", "Scheduled script started");
           
                var searchResult = salesorderSearchObj.run().getRange({
                    start: 0,
                    end: 1000
                });
                logme(searchResult.length);

                for (var j = 0; j < searchResult.length; j++) {
                    var singleResult = searchResult[j];
                    //getting values from search
                    //logme("singleResult",singleResult);
                    var internalid =singleResult.id;
                   

                    var applyingTransaction = singleResult.getValue({
                        name: 'applyingtransaction'

                    });

                    var soRecord = record.load({
                        type: "salesorder",
                        id: internalid
                    });

                    //function to get the invoice record count
                    var singleInvoice = findinvoiceCount(soRecord);

                    var lineCount = soRecord.getLineCount({
                        sublistId: 'item'
                    });

                    if (singleInvoice == 1) {

                        for (var i = 0; i < lineCount; i++) {


                            var type = soRecord.getSublistValue({
                                fieldId: "type",
                                sublistId: 'links',
                                line: i
                            });
                            //logme("type", type);

                            if (type == 'Invoice') {

                                //get the invoice id
                                var invoiceid = soRecord.getSublistValue({
                                    sublistId: 'links',
                                    fieldId: 'id',
                                    line: i
                                });
                                //logme("invoiceid", invoiceid);

                                //function to remove item line
                                var removeinv_itemline = removeinvoiceitem(invoiceid);
                                //logme("removeinv_itemline", removeinv_itemline);

                                //function to remove line item from So
                                removesoitem(soRecord, internalid);

                                //function to update quantity in So
                                updatequantity(soRecord, internalid);
                            }
                        }
                    }
                }
            } catch (er) {
                log.debug('error', er)
            }

        }

        return {
            execute: execute
        };


        /*******************************************************************************
         * Log these data
         * 
         * @param title
         * @param details
         * @returns
         * 
         * Created on 31-Jan-2019 by maria
         */
        function logme(title, details) {
            log.debug({
                title: title,
                details: details
            });
        }

        /*******************************************************************************
         * This function gets invoice count
         * 
         * @param record
         * @param itemid
         * 
         * Created on 29-Jan-2019 by maria
         */

        function findinvoiceCount(soRecord) {


            var numLines = soRecord.getLineCount({
                sublistId: 'links'
            });
            var count = 0;
            for (var i = 0; i < numLines; i++) {
                var type = soRecord.getSublistValue({
                    fieldId: "type",
                    sublistId: 'links',
                    line: i
                });
                if (type == "Invoice") {
                    count++;
                }
            }

            return count

        }

        /*******************************************************************************
         * return error
         * 
         * @param e
         * @returns
         * 
         * Created on 31-Jan-2019 by maria
         */
        function getError(e) {
            var stErrMsg = '';
            if (e.getDetails != undefined) {
                stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>' +
                    e.getStackTrace();
            } else {
                stErrMsg = '_' + e.toString();
            }
            return stErrMsg;
        }

        /*******************************************************************************
         * This function remove invoice line item
         * 
         * @param invoiceid
         * 
         * Created on 29-Jan-2019 by maria
         */
        function removeinvoiceitem(invoiceid) {

            var removeditem = [];

            var invid = record.load({
                type: record.Type.INVOICE,
                id: invoiceid
            });
            var invoicelineCount = invid.getLineCount({
                sublistId: 'item'
            });

            for (var j = 0; j < invoicelineCount; j++) {
                try {

                    var invquantityBilled = invid.getSublistValue({
                        fieldId: "quantity",
                        sublistId: 'item',
                        line: j
                    });

                    //logme("invquantityBilled", invquantityBilled);

                    if (invquantityBilled == 0) {

                        /*var itemid = invid.getSublistValue({
                            fieldId: "item",
                            sublistId: 'item',
                            line: j
                        });
                        removeditem.push(itemid);
*/

                        var removeLine = invid.removeLine({
                            sublistId: 'item',
                            line: j,
                            ignoreRecalc: true
                        });

                    }

                } catch (e) {
                    logme('TRY@subinv', getError(e));
                }
            }

            invid.save({
                enableSourcing: 'false',
                ignoreMandatoryFields: 'false'
            });
            //return removeditem
        }

        /*******************************************************************************
         * This function remove SO line item
         * 
         * @param soRecord
         * @param internalid
         *
         * Created on 29-Jan-2019 by maria
         */
        function removesoitem(soRecord, internalid) {
            var SOlineCount = soRecord.getLineCount({
                sublistId: 'item'
            });

            for (var j = 0; j < SOlineCount; j++) {
                try {

                    var quantityBilled = soRecord.getSublistValue({
                        fieldId: "quantitybilled",
                        sublistId: 'item',
                        line: j
                    });

                    if (quantityBilled == 0) {

                        //logme("success1");
                        var soRecord = record.load({
                            type: "salesorder",
                            id: internalid
                        });

                        var removeLine = soRecord.removeLine({
                            sublistId: 'item',
                            line: j,
                            ignoreRecalc: true
                        });
                        //logme("success2");

                        soRecord.save({
                            enableSourcing: 'false',
                            ignoreMandatoryFields: 'false'
                        });
                    }
                } catch (e) {
                    logme('TRY@updateSO', getError(e));
                }
            }
        }

        /*******************************************************************************
         * This function update quantity
         * 
         * @param soRecord
         * @param internalid
         *
         * Created on 29-Jan-2019 by maria
         */

        function updatequantity(soRecord, internalid) {

            var lineCount = soRecord.getLineCount({
                sublistId: 'item'
            });

            var soRecord = record.load({
                type: "salesorder",
                id: internalid
            });

            for (var i = 0; i < lineCount; i++) {

                try {

                    var quantity = soRecord.getSublistValue({
                        fieldId: "quantity",
                        sublistId: 'item',
                        line: i
                    });

                    var quantityBilled = soRecord.getSublistValue({
                        fieldId: "quantitybilled",
                        sublistId: 'item',
                        line: i
                    });

                    if (quantity != quantityBilled) {

                        //logme("success");
                        quantity = quantityBilled;
                        soRecord.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'quantity',
                            line: i,
                            value: quantityBilled
                        });
                    }

                } catch (e) {
                    logme('TRY@updatequantity', getError(e));
                }
            }
            soRecord.save({
                enableSourcing: 'false',
                ignoreMandatoryFields: 'false'
            });

        }

    });

Leave a comment

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