Suitelet to Enter Rejection Reason

A Suitelet can be used as a custom screen for entering a rejection reason as a part of the Approval flow and on hitting submit button the entered reason will be sent to the respective record. Workflow allows us to add custom actions (scripts) in GO TO RECORD action, or simply as a new action etc.

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
/****************************************************************************
 * Suitelet page for entering reason Estimate rejection.
 * **************************************************************************

 *
 *Description: The script help to open a suitelet page and user can enter reason 
  for rejection.
 *****************************************************************************
 **/
define(['N/record', 'N/https', 'N/ui/serverWidget', 'N/redirect'],

    function (record, https, serverWidget, redirect) {
        var main = {
            /****************************************************************************
            * CREATE THE ENTRY POINT OF THE SCRIPT.
            * FORM WRITE PART IS DONE
            * **************************************************************************/
            onRequest: function (scriptContext) {
                var form = main.custFormFunction(scriptContext);
                scriptContext.response.writePage(form);
            },
            /****************************************************************************
            * CREATE THE FORM.
            * SETIING VALUES TO FORM.
            * SETTING VALUE IN TO RECORD.
            * REDIRECT THE FORM.
            * FORM WRITE PART IS DONE
            * **************************************************************************/

            custFormFunction: function (scriptContext) {
                var tran_url = scriptContext.request.headers.referer
                var est_id = main.getParameterByName('id', tran_url);
                var form = serverWidget.createForm({ title: 'ESTIMATE REJECTION FORM', hideNavBar: false });
                form.addFieldGroup({ id: 'enterReason', label: 'ENTER THE REASON FOR REJECTION' });

                var reject_reason = form.addField({ id: 'custpage_reject_reason', type: serverWidget.FieldType.TEXTAREA, label: 'Enter the reason', container: 'enterReason' })
                reject_reason.isMandatory = true;
                reject_reason.updateDisplaySize({ height: 3, width: 50 });

                var est_rec_val = form.addField({ id: 'custpage_est_id', type: serverWidget.FieldType.TEXT, label: 'TXT', container: 'enterReason' })
                est_rec_val.updateDisplayType({ displayType: serverWidget.FieldDisplayType.HIDDEN });
                est_rec_val.defaultValue = est_id;

                form.addSubmitButton({ label: 'SUBMIT' });
                if (scriptContext.request.method == 'POST') {
                    var rejReason = scriptContext.request.parameters.custpage_reject_reason;
                    var estRecIdVal = scriptContext.request.parameters.custpage_est_id;
                    record.submitFields({ type: record.Type.ESTIMATE, id: estRecIdVal, values: { custbody_jj_est_reason_for_reject: rejReason }, options: { enableSourcing: false, ignoreMandatoryFields: true } })
                    redirect.toRecord({ type: record.Type.ESTIMATE, id: estRecIdVal });
                }

                return form;



            },

            /****************************************************************************
            * FUNCTION FOR GET RECORD ID FROM SCRIPT CONTECT URL.
            * **************************************************************************/

            getParameterByName: function (name, url) {
                if (!url)
                    return false;
                name = name.replace(/[\[\]]/g, "\\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                    results = regex
                        .exec(url);
                if (!results)
                    return null;
                if (!results[2])
                    return ' ';
                return decodeURIComponent(results[2].replace(/\+/g, " "));

            }


        }
        /****************************************************************************
        * FUNCTION FOR TRY CATCH.
        * **************************************************************************/

        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        };

        function trycatch(myfunction, key) {
            return function () {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("ERROR in  " + key, e);
                }
            }
        };
        return main;

    });

Leave a comment

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