SUITE SCRIPT TO CHANGE IMAGE NAME BY USER EVENT

This script is used to change the image name in the file cabinet after saving it in the format that is item name and the last 4 digits od series and the numbers

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/file', 'N/log', 'N/search'], 
  /**
   * @param {record} record
   * @param {file} file
   * @param {log} log
   * @param {search} search
   */
  (record, file, log, search) => {
    /**
     * Defines the function definition that is executed after the record is submitted.
     * @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) => {
      try {
        let currentRecord = scriptContext.newRecord;
        let itemId = currentRecord.getValue({
          fieldId: 'custrecord_jj_item_name_openboxlog1149'
        });

        let itemName;
        try {
          let fieldLookUp = search.lookupFields({
            type: search.Type.INVENTORY_ITEM,
            id: itemId,
            columns: ['itemid']
          });
          itemName = fieldLookUp.itemid;
        } catch (e) {
          log.error({
            title: 'Error looking up item name',
            details: e.message
          });
          return; // Exit the function if item lookup fails
        }

        log.debug('itemName', itemName);
        let serialNumber = currentRecord.getValue({
          fieldId: 'custrecord_jj_serial_no_openboxlog1149'
        });
        log.debug('serialNumber', serialNumber);
        let lastFourDigits = serialNumber.slice(-4);

        let updatedPrimaryImage = itemName + '-' + lastFourDigits + '-1';
        let updatedSecondaryImage = itemName + '-' + lastFourDigits + '-2';
        let updatedTertiaryImage = itemName + '-' + lastFourDigits + '-3';

        try {
          let primaryImageFileId = currentRecord.getValue({ fieldId: 'custrecord_jj_image_openboxlog1702' });
          if (primaryImageFileId) {
            let primaryImageFile = file.load({ id: primaryImageFileId });
            primaryImageFile.name = updatedPrimaryImage;
            primaryImageFile.save();
          }
        } catch (e) {
          log.error({
            title: 'Error updating primary image file',
            details: e.message
          });
        }

        try {
          let secondaryImageFileId = currentRecord.getValue({ fieldId: 'custrecord_jj_simage_openboxlog1702' });
          if (secondaryImageFileId) {
            let secondaryImageFile = file.load({ id: secondaryImageFileId });
            secondaryImageFile.name = updatedSecondaryImage;
            secondaryImageFile.save();
          }
        } catch (e) {
          log.error({
            title: 'Error updating secondary image file',
            details: e.message
          });
        }

        try {
          let tertiaryImageFileId = currentRecord.getValue({ fieldId: 'custrecord_jj_timage_openboxlog1702' });
          if (tertiaryImageFileId) {
            let tertiaryImageFile = file.load({ id: tertiaryImageFileId });
            tertiaryImageFile.name = updatedTertiaryImage;
            tertiaryImageFile.save();
          }
        } catch (e) {
          log.error({
            title: 'Error updating tertiary image file',
            details: e.message
          });
        }

      } catch (e) {
        log.error({
          title: 'Error in afterSubmit function',
          details: e.message
        });
      }
    }

    return { afterSubmit }
  }
);

Leave a comment

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