Scripting the Sales/Pricing tab in the item record.

The client (BGGN) would like to copy the costing details from the costing subtab to the sales/Pricing subtab in the item record.

We have created a user event script that will fetch the costing from the costing subtab and store them in the corresponding sales/Pricing details tab.

Initially, we will identify the internal id of the corresponding costing tab and then add the details to the pricing table.

/**

 * @NApiVersion 2.1

* @NScriptType UserEventScript

 */

/*************************************************************************************

 ***********

 * Bloom And Grow Ltd

 *

 * BGGN-1481 : Copy the SG and AU costing to item pricing tab

 *

 *

 *************************************************************************************

 ***********

 *

 *

 * Author: Jobin and Jismi IT Services LLP

 *

 * Date Created : 14-December-2023

 *

 * Description: Copy the price levels from the AU costing and SG costing subtab to pricing sublist.

 *

 * REVISION HISTORY

 *

 * @version 1.0 BGGN-1481: 14-December-2023 : Created the initial build by JJ0177

 *

 *

 *************************************************************************************

 **********/

 define([‘N/record’], (record) => {

    /**

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

       

        return parameter != null && parameter !== “” && parameter !== undefined && parameter !== “null” && parameter !== “undefined” && parameter !== ” “ && parameter !== false && parameter !== ;

    }

       /**

         * Function to get the field value.

         * @param {string} fieldId – fieldid

         * @param {object} record – The current item record      

         */

    function getValueOfField(fieldId, record) {

        try {

            return record.getValue({ fieldId });                      

        } catch (error)

        {

            log.error(“error@ getValueOfField”,error) ;

            return {};

        }      

    }

    /**

 * Function to set item pricing for a specific sublist and field.

 * @param {number} lineId – Line id of pricing

 * @param {object} itemRecord – The current item record

 * @param {string} sublistId – The sublist id where the pricing needs to be set

 * @param {string} fieldId – The field id corresponding to item pricing

 * @param {string} val – The value to set for item pricing

 */

function setItemPricing(lineId, itemRecord, sublistId,  val) {

    try {

        itemRecord.setSublistValue({

            sublistId: sublistId,

            fieldId: ‘price_1_’,

            line: lineId,

            value: val

        });

    } catch (error) {

        log.error(“error@setItemPricing”, error);

        return {}

    }

}

function setNullPricing(lineId, itemRecord, sublistId,){

    try {

        itemRecord.setSublistValue({

            sublistId: sublistId,

            fieldId: ‘price_1_’,

            line: lineId,

            value:

        });

    } catch (error) {

        log.error(“error@setItemPricing”, error);

        return {}

    }  

}

    /**

     * Defines the function definition that is executed after the 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 {

            let price6=‘price6’;

            let price8=‘price8’;

            if (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.type === scriptContext.UserEventType.EDIT) {

                let itemRecord = scriptContext.newRecord;

                  // fetch AU costing details

                let au30 = getValueOfField(‘custitem160’, itemRecord);

                let au35 = getValueOfField(‘custitem161’, itemRecord);                

                let au40 = getValueOfField(‘custitem82’, itemRecord);

                let auretail = getValueOfField(‘custitem171’, itemRecord);

                let auRRP = getValueOfField(‘custitem46’, itemRecord);

                let auWRP = getValueOfField(‘custitem89’, itemRecord);

                // fetch SG costing details

                let sg35 = getValueOfField(‘custitem65’, itemRecord);

                let sg36 = getValueOfField(‘custitem213’, itemRecord);

                let sg38 = getValueOfField(‘custitem156’, itemRecord);

                let sg39 = getValueOfField(‘custitem167’, itemRecord);

                let sg40 = getValueOfField(‘custitem71’, itemRecord);

                let sg42 = getValueOfField(‘custitem157’, itemRecord);

                let sg43 = getValueOfField(‘custitem158’, itemRecord);

                let sg45 = getValueOfField(‘custitem66’, itemRecord);

                let sg50 = getValueOfField(‘custitem67’, itemRecord);

                let sgRRP = getValueOfField(‘custitem49’, itemRecord);

                let objRecord = record.load({

                    type: record.Type.INVENTORY_ITEM,

                    id: itemRecord.id,

                });

                // setting AU pricing

                if (checkForParameter(au30)) {                  

                    setItemPricing(1, objRecord, price6,  au30);

                }

                else

                {

                    setNullPricing(1, objRecord, price6)

                }

                if (checkForParameter(au35)) {                  

                    setItemPricing(2, objRecord, price6,  au35);

                }  

                else

                {

                    setNullPricing(2, objRecord, price6)

                }        

                if (checkForParameter(au40)) {                

                    setItemPricing(3, objRecord, price6,  au40);

                }

                else

                {

                    setNullPricing(3, objRecord, price6)

                }

                if (checkForParameter(auretail)) {                

                    setItemPricing(10, objRecord, price6,  auretail);

                }

                else

                {

                    setNullPricing(10, objRecord, price6)

                }

                if (checkForParameter(auRRP)) {

                   setItemPricing(11, objRecord,price6,  auRRP);

                }

                else

                {

                    setNullPricing(11, objRecord, price6)

                }

                if (checkForParameter(auWRP)) {

                   setItemPricing(12, objRecord,price6,  auWRP);

                }

                else

                {

                    setNullPricing(12, objRecord, price6)

                }

                // setting SG pricing

                if (checkForParameter(sg35)) {

                   setItemPricing(122, objRecord, price8,  sg35);

                }

                else

                {

                    setNullPricing(122, objRecord, price8)

                }

                if (checkForParameter(sg36)) {

                   setItemPricing(123, objRecord, price8,  sg36);

                }

                else

                {

                    setNullPricing(123, objRecord, price8)

                }

                if (checkForParameter(sg38)) {

                   setItemPricing(124, objRecord, price8,  sg38);

                }

                else

                {

                    setNullPricing(124, objRecord, price8)

                }

                if (checkForParameter(sg39)) {

                    setItemPricing(125, objRecord, price8,  sg39);

                }

                else

                {

                    setNullPricing(125, objRecord, price8)

                }

                if (checkForParameter(sg40)) {

                    setItemPricing(126, objRecord, price8,  sg40);

                }

                else

                {

                    setNullPricing(126, objRecord, price8)

                }

                if (checkForParameter(sg42)) {

                    setItemPricing(127, objRecord, price8,  sg42);

                }

                else

                {

                    setNullPricing(127, objRecord, price8)

                }

                if (checkForParameter(sg43)) {

                    setItemPricing(128, objRecord, price8,  sg43);

                }

                else

                {

                    setNullPricing(128, objRecord, price8)

                }

                if (checkForParameter(sg45)) {

                    setItemPricing(129, objRecord, price8,  sg45);

                }

                else

                {

                    setNullPricing(129, objRecord, price8)

                }

                if (checkForParameter(sg50)) {

                    setItemPricing(130, objRecord, price8,  sg50);

                }  

                else

                {

                    setNullPricing(130, objRecord, price8)

                }      

                if (checkForParameter(sgRRP)) {

                   setItemPricing(137, objRecord, price8,  sgRRP);

                }

                else

                {

                    setNullPricing(137, objRecord, price8)

                }

                    objRecord.save({

                    enableSourcing: true,

                    ignoreMandatoryFields: true

                });

            }

        } catch (e) {

            log.error(“error @main function”, e);

        }

    };

    return { afterSubmit };

});

Leave a comment

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