Script to remove the Drawings/File attachments from the item record in a copy context

Jira Code: GTILC-397

/**
 * @NApiVersion 2.1
* @NScriptType UserEventScript
 */
/*************************************************************************************
 ***********
 * Graph Tech Guitar Labs
 *
 * GTILC-397 : Automation for adding drawings to its associated items
 *
 *
 *************************************************************************************
 ***********
 *
 *
 * Author: Jobin and Jismi IT Services LLP
 *
 * Date Created : 14-July-2023
 *
 * Description: This script is used to remove the drawings from the item record in make a copy context.
 *
 * REVISION HISTORY
 *
 * @version 1.0 GTILC-397 : 14-July-2023 : Created the initial build by JJ0177
 *
 *
 *************************************************************************************
 **********/
define(['N/record', 'N/search'],
    /**
     * @param{record} record
     * @param{search} search
     */
    (record, search,) => {
        /**
         * Function to check whether the field has an empty value or not.
         * @param {parameter} parameter - fieldValue
         * @returns {boolean} true - if the value is not empty
         * @returns {boolean} false - if the value is empty
         */
        function checkForParameter(parameter) {
            try {
                if (parameter != "" && parameter != null && parameter != undefined && parameter != "null" && parameter != "undefined" && parameter != " " && parameter != false && parameter != '' && parameter != ' ') {
                    return true;
                } else {
                    return false;
                }
            }
            catch (e) {
                log.debug({
                    title: "Error @ empty check Function: ", details: e.name + ' : ' + e.message
                });

            }
        }
        /**
         * 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 {
                if (scriptContext.type === scriptContext.UserEventType.COPY) {
                    let itemRecord = scriptContext.newRecord;
                    itemRecord.setValue({
                        fieldId: 'custitem_jj_is_attached_gtilc_397',
                        value: '',
                        ignoreFieldChange: true
                    });
                    let fileAttachmtCount = itemRecord.getLineCount('mediaitem');
                    if (checkForParameter(fileAttachmtCount)) {
                        for (let i = 0; i < fileAttachmtCount; i++) {
                            let mediaitemValue = itemRecord.getSublistValue({
                                sublistId: 'mediaitem',
                                fieldId: 'mediaitem',
                                line: i
                            });
                            itemRecord.removeLine({
                                sublistId: 'mediaitem',
                                line: i
                            });
                        }
                    }
                }

            } catch (e) {
                log.error("error @main function", e);
            }
        }
        return { beforeLoad }
    });

Leave a comment

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