Set Related item fulfillment in the Invoice

The following script can be used to set the item fulfillment number in the invoice when the user creates invoice by clicking Bill button in the item fulfillment record.

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

define([],
    /**
     * @param{record} record
     */
    function () {

        /**
         * This function is used to get the Item fulfillment id from URL
         * @returns Item fulfillment id
         */
        function getItemFulfillmentId() {
            try {
                let invUrl = window.location.search;
                let parameters = invUrl.split("&");
                for (let i = 0; i < parameters.length; i++) {
                    let parameterPair = parameters[i].split("=");
                    if (parameterPair[0] == 'itemship') {
                        return decodeURIComponent(parameterPair[1]);
                    }
                }
            } catch (e) {
                log.error('error @ getItemFulfillmentId', e);
                return '';
            }
        }

        /**
         * 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) {
            try {
                if (scriptContext.mode == 'copy') {
                    let itemFulfillmentId = getItemFulfillmentId();
                    let currentRec = scriptContext.currentRecord;
                    if (itemFulfillmentId) {
                        currentRec.setValue({
                            fieldId: 'custbody_jj_related_item_fulfillment',
                            value: itemFulfillmentId
                        });
                    } else {
                        currentRec.setValue({
                            fieldId: 'custbody_jj_related_item_fulfillment',
                            value: null
                        });
                    }
                }
            } catch (e) {
                log.error('error @ pageInit', e);
            }
        }

        return {
            pageInit: pageInit,
        };

    });

Leave a comment

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