Storing Item Receipt number in Bill and Bill number in Item Receipt

Requirement

There is a bill button on the IR record and if the user clicks the bill button from the IR record, the IR number should populate to the custom field in the bill record. And once the user saves the bill, the corresponding bill number need to be stored in a custom field in the IR record.

Solution

Create two custom fields to store the Item Receipt Number in Bill and the Bill Number in Item Receipt.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

define(['N/currentRecord', 'N/record', 'N/runtime', 'N/search'],
    /**
 * @param{currentRecord} currentRecord
 * @param{record} record
 * @param{runtime} runtime
 * @param{search} search
 */
    (currentRecord, record, runtime, search) => {
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */


        const beforeLoad = (scriptContext) => {
               try{
                   var parms = scriptContext.request.parameters.itemrcpt;//To get the itemreceipt number from url
                    log.debug('param',parms)
                   var current_rec=scriptContext.newRecord;

                    //Setting the itemreceipt number in custom field IR number
                   current_rec.setValue({
                       fieldId:'custbody_ir_number',
                       value:parms
                   })
                }
        catch (e){
               log.debug('Error at before load',e)
               }

        }

        const beforeSubmit = (scriptContext) => {

        }

        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
            try {

                var current_record = scriptContext.newRecord;
                var rec_id = scriptContext.newRecord.id;

                //Getting the IR number stored in the custom field IR number
                var ir_id=current_record.getValue({
                    fieldId:'custbody_ir_number'
                })

                //Setting the IR number in the custom field Bill Number
                 record.submitFields({
                    type: record.Type.ITEM_RECEIPT,
                    id:ir_id ,
                    values: {
                        'custbody_bill_no': rec_id
                    }
                });
            }
            catch (e) {
                log.debug('Error in aftersubmit',e)
            }

        }

        return {beforeLoad, afterSubmit}

    });

Leave a comment

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