Script to add a button in Quote Record

Jira Code : CLTG-177

Description

We need to create a custom button named “SOP” in the Quote record and on clicking that button, user should be navigated to a URL in a new window. We need to be redirected to different webpages based on which form the button is on.

Solution

In order to achieve this, we need to have a User Event script (to add the button ) along with a Client Script (for the working of the button). We have a custom record where it stores the custom form’s internal id along with the URL.

JJ UE Add SOP Button in Quote CLTG-177

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/************************************************************************************************
 *  CLTG-177 >>  To view the SOP Button in the Quote record **
 *
 * **********************************************************************************************
 *
 * Author: Cleerline Technology Group-USA-NS/SCA
 *
 * Date Created : 13-JAnuary-2023
 *
 * Created By: Aranya T R , Jobin and Jismi IT Services LLP
 *
 * REVISION HISTORY
 *
 ***********************************************************************************************/
define(['N/record', 'N/search'],
    /**
     * @param{record} record
     * @param{search} search
     */
    (record, search) => {

        /**
         * Function to search for the URL corresponding to each form from QUOTE
         * @param formID
         * @return {webURL} webURL
        * */
        function SOP_URL_MappingSearch(formID)
        {
            try
            {
                let customrecord_jj_sop_btn_mappings_cltg177SearchObj = search.create({
                    type: "customrecord_jj_sop_btn_mappings_cltg177",
                    filters:
                        [
                            ["custrecord_jj_quote_form_id","equalto",formID],
                            "AND",
                            ["isinactive", "is","F"]
                        ],
                    columns:
                        [
                            search.createColumn({name: "custrecord_jj_sop_url", label: "URL"})
                        ]
                });
                let searchResultCount = customrecord_jj_sop_btn_mappings_cltg177SearchObj.runPaged().count;

                let webURL ;
                if (searchResultCount > 0)
                {
                    customrecord_jj_sop_btn_mappings_cltg177SearchObj.run().each(function (result)
                    {
                        webURL = result.getValue({ name: "custrecord_jj_sop_url", label: "URL" });
                    });
                    log.debug("webURL", webURL)
                    return webURL ;
                }
            }
            catch (e)
            {
                log.debug("Error @SOP_URL_MappingSearch ", e);
            }
        }

        /**
         * 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
            {
                let quoteID = scriptContext.newRecord.id;
                const form = scriptContext.form;

                if (scriptContext.type == "view")
                {
                    /** Defining Client script ID **/
                    form.clientScriptFileId = 848559;

                    let quoteRec = record.load({
                        type : record.Type.ESTIMATE,
                        id   : quoteID
                    });

                    let formID = quoteRec.getValue("customform");
                    let URL = SOP_URL_MappingSearch(formID);

                   //Adding SOP Button
                    form.addButton({
                        id: 'custpage_sopbutton',
                        label: 'SOP',
                        functionName: 'onSOPClick("'+URL+'")'
                    });
                }
            }
            catch (e)
            {
                log.error("Error @ beforeLoad", e)
            }
        }

        return {beforeLoad}

    });

JJ CS SOP Button in Quote CLTG-177

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define([],

    function() {

        /**
         * Function to open web pages in new tab on clicking the SOP button
         * @param {url} url
         */
        function onSOPClick(url)
        {
            try
            {
                //Open Quote in new Tab
                window.open(url, "_blank");
                return true;
            }
            catch (e)
            {
                log.error("Error @onSOPClick", e);
                alert("Invalid URL");
                return true;
            }
        }

        /**
         * Function to be executed after page is initialized.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
         *
         * @since 2015.2
         */
        function pageInit(scriptContext)
        {
            console.log("Page is intialized")
            return true;
        }

        return {
            pageInit: pageInit,
            onSOPClick : onSOPClick
        };

    });

Leave a comment

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