Suite Script: Item receipt Automation for transfer order when item fulfillment is shipped

define([‘N/email’, ‘N/error’, ‘N/record’, ‘N/runtime’, ‘N/search’],

 /**

* @param{email} email

* @param{error} error

* @param{record} record

* @param{record} record

* @param{runtime} runtime

*/

 (email, error, record, runtime, search) => {

     ‘use strict’;

/**

* Updates the Item Fulfillment record with the reference to the created Item Receipt.

*

* @param {number} itemReceiptId – The ID of the created Item Receipt.

* @param {number} itemFulfillmentId – The ID of the Item Fulfillment record to be updated.

*/

     function submitIrInIf(itemReceiptId, itemFulfillmentId) {

         try {

             record.submitFields({

                 type: record.Type.ITEM_FULFILLMENT,

                 id: itemFulfillmentId,

                 values: {

                     ‘custbody_jj_item_receipt_ref’: itemReceiptId

                 },

                 options: {

                     enableSourcing: false,

                     ignoreMandatoryFields: true

                 }

             });

         } catch (e) {

             log.error(‘Error updating Item Fulfillment’, e.message);

         }

     }

     /**

      * Validates and sets the quantities for the Item Receipt lines based on the Item Fulfillment record.

      *

      * @param {Object} itemReceipt – The Item Receipt record object.

      * @param {Object} newRecord – The Item Fulfillment record object.

      */

     function setItemReceiptQuantities(itemReceipt, newRecord) {

        try {

            let lineCount = newRecord.getLineCount({ sublistId: ‘item’ });

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

                let itemId = newRecord.getSublistValue({ sublistId: ‘item’, fieldId: ‘item’, line: i });

                let quantity = newRecord.getSublistValue({ sublistId: ‘item’, fieldId: ‘quantity’, line: i });

                let receiptLine = itemReceipt.findSublistLineWithValue({

                    sublistId: ‘item’,

                    fieldId: ‘item’,

                    value: itemId

                });

                if (receiptLine !== 1) {

                    itemReceipt.selectLine({ sublistId: ‘item’, line: receiptLine });

                    itemReceipt.setCurrentSublistValue({

                        sublistId: ‘item’,

                        fieldId: ‘quantity’,

                        value: quantity

                    });

                    itemReceipt.commitLine({ sublistId: ‘item’ });

                }

            }

        } catch (e) {

            log.error(‘Error updating Item Fulfillment’, e.message);

        }

    }

      /**

    * Validates the record and its origin.

    *

    * @param {Object} newRecord – The new record object.

    * @param {number} createdFromId – The ID of the record that this Item Fulfillment is created from.

    * @returns {boolean} – Returns true if the validation passes, otherwise false.

    */

      function validatesCreatedFrmRecord(newRecord, createdFromId) {

        try {

            if (newRecord.type !== record.Type.ITEM_FULFILLMENT) {

                return;

            }

            if (!createdFromId) {

                return;

            }

            let lookupFields = search.lookupFields({

                type: search.Type.TRANSACTION,

                id: createdFromId,

                columns: [‘type’]

            });

            let createdFromType = lookupFields.type[0].text;

            return createdFromType === “Transfer Order”;

        } catch (e) {

            return;

        }

    }

     /**

      * 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) => {

         let newRecord = scriptContext.newRecord;

         if (scriptContext.type !== scriptContext.UserEventType.CREATE && scriptContext.type !== scriptContext.UserEventType.SHIP) {

             return;

         }

         let createdFromId = newRecord.getValue({ fieldId: ‘createdfrom’ });

         validatesCreatedFrmRecord(newRecord, createdFromId);

         try {

             let fulfillmentDataObj = {

                 “status”: newRecord.getValue({ fieldId: ‘shipstatus’ }),

                 “existingReceiptRef”: newRecord.getValue({ fieldId: ‘custbody_jj_item_receipt_ref’ }),

                 “itemFulfillmentId”: newRecord.id

             }

             if (fulfillmentDataObj.existingReceiptRef) {

                 log.debug(‘Item Receipt already created’, ‘Item Receipt ID: ‘ + fulfillmentDataObj.existingReceiptRef);

                 return;

             }

             if (fulfillmentDataObj.status === ‘C’) {

                 let itemReceipt = record.transform({

                     fromType: record.Type.TRANSFER_ORDER,

                     fromId: createdFromId,

                     toType: record.Type.ITEM_RECEIPT,

                     isDynamic: true

                 });

                 let fulfillmentDate = newRecord.getValue({ fieldId: ‘trandate’ });

                 let currentDate = new Date();

                 let receiptDate = fulfillmentDate > currentDate ? fulfillmentDate : currentDate;

                 itemReceipt.setValue({

                     fieldId: ‘trandate’,

                     value: receiptDate

                 });

                 setItemReceiptQuantities(itemReceipt, newRecord);

                 let itemReceiptId = itemReceipt.save({

                     enableSourcing: true,

                     ignoreMandatoryFields: false

                 });

                 submitIrInIf(itemReceiptId, fulfillmentDataObj.itemFulfillmentId);

                 log.debug(‘Item Receipt Created’, ‘Item Receipt ID: ‘ + itemReceiptId);

                }

         } catch (e) {

             log.error(‘Error creating Item Receipt’, e.message);

         }

     }

     return { afterSubmit }

 });

Leave a comment

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