Block expense report approval

Jira Code : SMT-17

Description:

Use a script to block the approval process through the standard page “Approve expense report” based on specific conditions. If the report is created by the Team Lead role, it should remain unapproved. If the approver’s role is “Team Lead” and should be approved for the other roles Approve user, manager user & superuser.

If the created user role is Approve user, it can be approved only by the other approve user/manager user/superuser.

If the created user role is Manager user, it can be approved only by other manager users/ superuser.

 /**

     * @NApiVersion 2.x

     * @NScriptType UserEventScript

     * @NModuleScope SameAccount

     */

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

     * SMT-17 UE Block Expense Report Approval

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

     *

     * Date: 18-06-2019

     *

     * Author: Jobin & Jismi IT Services LLP

     *

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

     **/

define([‘N/runtime’, ‘N/record’],

    function(runtime, record) {

        function afterSubmit(scriptContext) {

            try {

                var recordId = scriptContext.newRecord.id;

                log.debug(“scriptContext.mode”, scriptContext.type);

                var userObj = runtime.getCurrentUser();

                var userRole = userObj.roleId;

                log.debug(“Current user role”, userRole);

                var currentUserRole = userObj.role;

                log.debug(“Current user role id”, userObj.role);

                var currentUser = userObj.name;

                log.debug(“Current user name”, currentUser);

                log.debug(“Current user name id”, userObj.id);

                var createdRole = scriptContext.newRecord.getValue({

                    fieldId: ‘custbody_jj_smt_11_role’

                });

                log.debug(“Created Role id”, createdRole);

                var createdUser = scriptContext.newRecord.getValue({

                    fieldId: ‘custbody_jj_smt_11_user’

                });

                log.debug(“Created User id”, createdUser);

                var oldValue = scriptContext.oldRecord.getValue({

                    fieldId: ‘accountingapproval’

                });

                log.debug(“oldValue”, oldValue);

                if (scriptContext.type == “approve”) {

                    if (((createdRole == 1036) && ((currentUserRole == 1049) || (currentUserRole == 1048) || (currentUserRole == 1037) || (currentUserRole == 3))) ||

                        ((createdRole == 1049) && ((currentUserRole == 1048) || (currentUserRole == 1037) || (currentUserRole == 3)) || ((currentUserRole == 1049) && (userObj.id != createdUser))) ||

                        ((createdRole == 1048) && ((currentUserRole == 1037) || (currentUserRole == 3)) || ((currentUserRole == 1048) && (userObj.id != createdUser))) ||

                        ((createdRole == 1037) && ((currentUserRole == 1037) || (currentUserRole == 3)))) {

                        log.debug(“Allow approval case”);

                    } else {

                        log.debug(“Block approval case”);

                        var expenseReportEdit = record.load({

                            type: “expensereport”,

                            id: recordId,

                            isDynamic: false

                        });

                        expenseReportEdit.setValue({

                            fieldId: ‘accountingapproval’,

                            value: false

                        });

                        var recordIdSales = expenseReportEdit.save();

            } catch (err) {

                log.debug({

                    title: ‘err’,

                    details: err

                });

            }

        }

        return {

            afterSubmit: afterSubmit

        };

    });

Leave a comment

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