Scenario
How to restrict the sales order type warranty order from all the roles other than Admin and Accounting Manager
Solution
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/************************************************************************************************
* AHAP -896 Warranty Order Script
*********************************************************************************************
*
* Author: Jobin and Jismi IT Services
* Date Created : 29-March-2023
* Instance: AHAP Sandbox
* Created By: Aswathy, Jobin and Jismi IT Services
* Description : Script for Restricting Warranty Order
*
* REVISION HISTORY
* Revision 1.0 - 13 April 2023 - Warranty Order Script
***********************************************************************************************/
define(['N/currentRecord', 'N/record','N/runtime'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param {runtime}runtime
*/
function(currentRecord, record,runtime) {
/**
* Defines the Roles
* @type {{ACCOUNTING_MANAGER: string, AHA_LOGISTIC_BASIC: string, AHA_LOGISTIC_MASTER: string, ADMIN: string}}
*/
const ROLES_OBJECT ={
ADMIN:3,
ACCOUNTING_MANAGER :1099,
AHA_LOGISTIC_BASIC:1115,
AHA_LOGISTIC_MASTER:1065
}
/**
* Defines the sales order type
* @type {{REGULAR_ORDER: number, WARRANTY_ORDER: string}}
*/
const SALES_ORDER_TYE_OBJECT ={
WARRANTY_ORDER :'4',
REGULAR_ORDER :1,
}
/**
* 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("invoice page init")
}
/**
* Validation function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @returns {boolean} Return true if field is valid
*
* @since 2015.2
*/
function validateField(scriptContext) {
try
{
var currentRec=scriptContext.currentRecord;
// var sublistFieldName = scriptContext.fieldId;
var soType = currentRec.getValue({
fieldId: 'custbody52'
});
var currentRole = runtime.getCurrentUser().role
if(scriptContext.fieldId == 'custbody52'){
if(currentRole != ROLES_OBJECT.ADMIN && currentRole != ROLES_OBJECT.ACCOUNTING_MANAGER && currentRole != ROLES_OBJECT.AHA_LOGISTIC_MASTER && currentRole != ROLES_OBJECT.AHA_LOGISTIC_BASIC)
{
if(soType == SALES_ORDER_TYE_OBJECT.WARRANTY_ORDER)
{
alert("You do not have the permission to choose the sales order type as Warranty Order.")
currentRec.setValue({
fieldId: 'custbody52',
value: SALES_ORDER_TYE_OBJECT.REGULAR_ORDER
});
return false;
}
}
}
return true;
}
catch(err)
{
console.log("error@validateField",err)
}
}
return {
pageInit:pageInit,
validateField: validateField,
};
});