Suitelet for Approval through Email

The approver who don’t have access in netsuite need to approve or reject the purchase order through email by clicking the approve or reject link

Condition 1: By using suitelet, If the approver click the approve link it first check whether the PO is already approved or not. If it is already approved it shows the message ‘PO has already approved ‘. Or Otherwise if the PO is not approved, it redirect to the page where we need to confirm whether we need to approve or not?. By clicking confirm button, it will set approval status as ‘Approved’ and ‘Approver’ and it also shows ‘PO status has been Approved’.

Condition 2: If the approver click the Reject link, it will redirect to the page where we need to confirm whether we need to reject or Cancel?

  • By clicking confirm button, it will set approval status as ‘Rejected’ and it also shows ‘PO status has been Rejected’.
  • By clicking cancel button it will close the current window
/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
//var CLIENT_SCRIPT_FILE_ID = 2645;
define(['N/ui/serverWidget', 'N/record', 'N/action', 'N/auth', 'N/cache', 'N/config', 'N/email', 'N/error', 'N/file', 'N/format', 'N/http', 'N/https', 'N/record', 'N/redirect', 'N/render', 'N/runtime', 'N/search', 'N/ui/dialog', 'N/ui/message', 'N/url'],
    /**
     * @param{action} action
     * @param{auth} auth
     * @param{cache} cache
     * @param{config} config
     * @param{currentRecord} currentRecord
     * @param{email} email
     * @param{error} error
     * @param{file} file
     * @param{format} format
     * @param{http} http
     * @param{https} https
     * @param{record} record
     * @param{redirect} redirect
     * @param{render} render
     * @param{runtime} runtime
     * @param{search} search
     * @param{dialog} dialog
     * @param{message} message
     * @param{url} url
     */
    (serverWidget, record, action, auth, cache, config, email, error, file, format, http, https, redirect, render, runtime, search, dialog, message, url) => {
        /**
         * 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) => {
            if (context.request.method === 'GET') {

                var approverType = context.request.parameters.type_status;
             
                var po_id = context.request.parameters.po_id;              
                var po_id = context.request.parameters.po_id;
                var approver_id = context.request.parameters.approver_id;
                var approverType = context.request.parameters.type_status;
                //load the purchase order
                log.debug("PO load");

                var objRecord = record.load({
                    type: record.Type.PURCHASE_ORDER,
                    id: po_id
                });



                var value = objRecord.getValue({
                    fieldId: 'approvalstatus'
                });
                //In 'approval_status' custom field is already set to be  'Approve'
                if (value == '2') {
                    var form = serverWidget.createForm({
                        title: 'The Purchase Order has been approved.'
                    });
                }

                //If 'approval_status' custom field is already set to be Rejected
                else if (value == '3') {
                    var form = serverWidget.createForm({
                        title: 'The Purchase Order has been Rejected.'
                    });
                }

                else {
                    log.debug("PendingApproval")
                    if (approverType == 'approve') {
                        var form = serverWidget.createForm({
                            title: 'Do you want to approve purchase order?'
                        });
                        form.clientScriptFileId = 2645 //as noted in step 1
                        var field = form.addField({
                            id: 'custpage_poid',
                            type: serverWidget.FieldType.TEXT,
                            label: 'PO number'
                        });

                        field.defaultValue = po_id;
                        field.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.HIDDEN
                        });

                        var poApproverId = form.addField({
                            id: 'custpage_approverid',
                            type: serverWidget.FieldType.TEXT,
                            label: 'Approver Id'
                        });
                        poApproverId.defaultValue = approver_id;
                        poApproverId.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.HIDDEN
                        });

                        var poType = form.addField({
                            id: 'custpage_approvertype',
                            type: serverWidget.FieldType.TEXT,
                            label: 'Type'
                        });
                        poType.defaultValue = approverType;
                        poType.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.HIDDEN
                        });
                        form.addSubmitButton({

                            label: 'Confirm'

                        });
                        form.addButton({
                            id: 'custbutton_cancel',
                            label: 'Cancel',
                            functionName: 'cancelPo'
                        });

                    } else {
                        var form = serverWidget.createForm({
                            title: 'Do you want to reject the purchase order?'
                        });
                        form.clientScriptFileId = 2645 //as noted in step 1
                        // form.clientScriptFileId = CLIENT_SCRIPT_FILE_ID;
                        var field = form.addField({
                            id: 'custpage_poid',
                            type: serverWidget.FieldType.TEXT,
                            label: 'PO number'
                        });
                        field.defaultValue = po_id;
                        field.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.HIDDEN
                        });
                        var poApproverId = form.addField({
                            id: 'custpage_approverid',
                            type: serverWidget.FieldType.TEXT,
                            label: 'Approver Id'
                        });
                        poApproverId.defaultValue = approver_id;
                        poApproverId.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.HIDDEN
                        });

                        var poType = form.addField({
                            id: 'custpage_approvertype',
                            type: serverWidget.FieldType.TEXT,
                            label: 'Type'
                        });
                        poType.defaultValue = approverType ;
                        poType.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.HIDDEN
                        });

                        form.addSubmitButton({
                            label: 'Confirm'
                        })
                        form.addButton({
                            id: 'custbutton_cancel',
                            label: 'Cancel',
                            functionName: 'cancelPo'
                        });

                    }
                }



                context.response.writePage(form);
            }
            //action on click of the submit button
            if (context.request.method === 'POST'){


                // var request = scriptContext.request;
                var po_id = context.request.parameters.custpage_poid;

                var approver_id = context.request.parameters.custpage_approverid;
                var approver_Type = context.request.parameters.custpage_approvertype;

                if (approver_Type == "approve"){
                    var otherId = record.submitFields({
                        type: record.Type.PURCHASE_ORDER,
                        id: Number(po_id),
                        values: {
                            'approvalstatus': '2',
                            'custbody_cl_approver_clgc_721': approver_id
                        }
                    });
                    var objRecordvalue = record.load({
                        type: record.Type.PURCHASE_ORDER,
                        id: po_id
                    });
                    var tranid = objRecordvalue.getValue({
                        fieldId: 'tranid'
                    });
                    var createdby = objRecordvalue.getValue({
                        fieldId: 'custbodycl_createdby_clgc_721'
                    });
                    var EmployeeId = objRecordvalue.getText({
                        fieldId: 'custbody_cl_approver_clgc_721'
                    });

                    email.send({
                        author: approver_id,
                        recipients: createdby,
                        subject: "PO Approved",
                        body: 'Hi,<br />'+'Purchase order ' +tranid+ ' has been approved by ' + EmployeeId +'.<br />'+'Thanks',
                        relatedRecords: {
                            transactionId: Number(po_id)
                        }
                    });
                    var form = serverWidget.createForm({
                        title: 'The Purchase Order has been Approved.'
                    });

                }
                else{
                    var otherId = record.submitFields({
                        type:  record.Type.PURCHASE_ORDER,
                        id: po_id,
                        values: {
                            'approvalstatus': '3'
                        }
                    });
                    var objRecordvalue = record.load({
                        type: record.Type.PURCHASE_ORDER,
                        id: po_id
                    });
                    var tranid = objRecordvalue.getValue({
                        fieldId: 'tranid'
                    });
                    var createdby = objRecordvalue.getValue({
                        fieldId: 'custbodycl_createdby_clgc_721'
                    });
                    email.send({
                        author:approver_id,
                        recipients: createdby,
                        subject: "PO Rejected",
                        body: '<div>Hi,<br />'+'The Purchase Order ' + tranid + ' has been Rejected.<br />'+'Please review and Resubmit for approval.<br />'+'Thanks</div>',
                        relatedRecords: {
                            transactionId: Number(po_id)
                        }
                    });
                    var form = serverWidget.createForm({
                        title: 'The Purchase Order has been Rejected.'
                    });
                }
                context.response.writePage(form);
            }



        }



        return {
            onRequest: onRequest
        }

    });
**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/action', 'N/currentRecord', 'N/error', 'N/http', 'N/https', 'N/log', 'N/record'],
/**
 * @param{action} action
 * @param{currentRecord} currentRecord
 * @param{error} error
  * @param{http} http
 * @param{https} https
 * @param{log} log
 * @param{record} record
  */
function(action, currentRecord, error, http, https, log, record, runtime, search, dialog, message, url) {
    
    /**
     * 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) {

    }



    function cancelPo() {
        window.close();

    }


    return {

        pageInit: pageInit,
        cancelPo: cancelPo
    };
    
});

Leave a comment

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