Update Latest Invoice ID in the Item Fulfillment Record

There will be a custom transaction body field in the item fulfillment record with the label “INVOICE ID”, want to update the latest created invoice id in the field. A UserEvent script is created to deploy in the invoice record, On creating the new invoice the script works.

In Addition, a field is added in the invoice record to set the item fulfillment id in “after submit” entry-point.

Risk:-

We will be updating the invoice ID field value in the latest item fulfillment created with shipped status. Suppose the user created the second item fulfillment record and then they created the invoice for the first item fulfillment record, then the update in the Invoice ID field will be mistaken. Invoice ID field will not be get updated in the existing item fulfillment records.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*************************************************************
 * CLIENTNAME: FURTEX LIMITED
 * FL-35
 * Invoice ID Custom Field Update
 * **********************************************************
 * Date :27-01-2022
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description :
 * The script is used to  update a custom field "InvoiceId" in the item fulfillment record,
 * when a new invoice is created corresponding to that.
 * Date created :24-01-2022
 * Created by: Sandhra Simon, Jobin & Jismi IT Services LLP
 * REVISION HISTORY
 * Revision 1.0 ${27-01-2022}
 *
 */
define(['N/search','N/record'],
    /**
     * @param{search} search
     * @param{record} record
     */
    (search,record) => {
        /**
         * Defines the function definition that is executed after 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 afterSubmit = (scriptContext) => {

                if(scriptContext.type === scriptContext.UserEventType.CREATE) {
                    let salesOrderId = scriptContext.newRecord.getValue("createdfrom");
                    
                    let recordId = scriptContext.newRecord.id
                    
                    var invoiceRecord = record.load({
                        type: record.Type.INVOICE,
                        id: recordId,
                        isDynamic: true
                    })
                    var invoiceId = invoiceRecord.getValue({
                        fieldId: 'tranid'
                    })
                    if (salesOrderId) {
                        let itemShipIdSearch = search.create({
                            type: "transaction",
                            filters: [
                                ["mainline", "is", "T"],
                                "AND",
                                ["type", "anyof", "ItemShip"],
                                "AND",
                                ["createdfrom", "anyof", salesOrderId]
                            ],
                            columns:
                                [
                                    search.createColumn({name: "internalid", label: "Internal ID"}),
                                    search.createColumn({
                                        name: "datecreated",
                                        sort: search.Sort.DESC
                                    })
                                ]
                        })
                        var itemFulfillmentInternalId;
                        itemShipIdSearch.run().each((result) => {
                            itemFulfillmentInternalId = result.getValue({name: "internalid", label: "Internal ID"})
                            return false;
                        })

                        if (itemShipIdSearch) {
                            record.submitFields({
                                type: record.Type.ITEM_FULFILLMENT,
                                id: itemFulfillmentInternalId,
                                values: {
                                    'custbody_jj_invoice_id': invoiceId
                                }
                            })
                        }
                    }
                    var itemfulfillmentIdInInvoice = invoiceRecord.setValue({
                        fieldId: 'custbody_jj_item_fulfillment_fl_37',
                        value: itemFulfillmentInternalId,
                        ignoreFieldChange: true
                    })

                    invoiceRecord.save();
                }

        }
        return{afterSubmit}
    });

Leave a comment

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