Suitelet to handle Approve and Reject Buttons from email template

Releated KB:

https://jobinandjismi.in/email-template-with-approve-and-reject-buttons/

This suitelet is used to approve the corresponding vendor bill which is send through email

define(['N/record', 'N/ui/serverWidget', 'N/http'],
    /**
 * @param{record} record
 * @param{serverWidget} serverWidget
 * @param{http} http
 */
    (record, serverWidget, http) => {
        /**
         * Defines the Suitelet script trigger point.
         * @param {Object} scriptContext
         * @param {ServerRequest} scriptContext.request - Incoming request
         * @param {ServerResponse} scriptContext.response - Suitelet response
         * @since 2015.2
         */
        const onRequest = (context) => {
            try {

                if (context.request.method === 'GET') {
                    // Retrieve the purchase order ID from the query parameters
                    const poId = context.request.parameters.po_id;
                    const status = context.request.parameters.btn;
                    const approve = '2';
                    const reject = '3';
                    let billStatus, msg;
                    

                    if (status == 'true') {
                        // Set the approval status of the purchase invoice to 'Approved'
                        billStatus = approve;
                        msg = "The Purchase Invoice has been Approved";

                    } else {
                        // Set the approval status of the purchase invoice to 'Rejected'
                        billStatus = reject;
                        msg = "The Purchase Invoice has been Rejected";
                    }

                    // Save the purchase order record
                    record.submitFields({
                        type: record.Type.VENDOR_BILL,
                        id: poId,
                        values: {
                            'approvalstatus': billStatus
                        },
                        options: {
                            ignoreMandatoryFields: true
                        }
                    });

                    context.response.sendRedirect({
                        type: http.RedirectType.RECORD,
                        identifier: record.Type.VENDOR_BILL,
                        id: poId
                    });
                }
            } catch (error) {
                log.error("Err@reqst",error.message)

            }

        }

        return { onRequest }

    });

Leave a comment

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