Jira code: CORP-11
When the price level in SO sublist changes, the SO will be under commission approval process. On record submit, mail will be sent to the Sales rep and VP. Also commission approval status will be ‘pending approval’. The current SO will not be editable. In sales rep mail, added a approve button to approve it. On click, will redirect to a new tab and shows a message. Now the sales rep approval completes and in SO, commission approval status will be ‘sales rep approved’. After sales rep approval, mail sends to VP. VP can be logged to Netsuite and approve it. Now for VP, the SO is editable. So now VP can edit the SO and update commission percent. In SO record there is a button ‘Commission Approve’. Once VP edit SO and update commission %, after saving the record, VP needs to click ‘commission approve’ button to complete the entire commission approval process. After VP approval the commission approval status will be VP approved. Once ‘commission approve button’ clicked if commission % field is not updated a popup comes to show whether SO ‘sales rep commission %’ update required or not. If not, click the ‘OK’ button. If needed to edit SO, cancel the popup and update SO.Once ‘commission approve’ button clicked the commission approval process completes and the SO is editable to all.
User Event script:
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/**
* Script Description
* In SO, when item price level change an approval procces is required for the SO for further processing.
.
*
*/
/*******************************************************************************
* CORP DESIGN
* **************************************************************************
*
* Date: 23/05/2019
*
* Author: Jobin & Jismi IT Services LLP
*
*
* REVISION HISTORY
*
* Revision $ 23/05/2019 Maria: Create
*
******************************************************************************/
define(['N/record','N/search','N/email','N/render', 'N/runtime','N/url'],
function(record,search,email,render,runtime,url) {
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {string} scriptContext.type - Trigger type
* @param {Form} scriptContext.form - Current form
* @Since 2015.2
*/
function beforeLoad(scriptContext) {
try{
var SO_id = scriptContext.newRecord;
var CustRec=scriptContext.form;
//When change in price level, this field is checked.
var pricelevelcheckbox = scriptContext.newRecord.getValue({
fieldId: 'custbody_cd12_jj_price_level_change'
});
var commission_status = scriptContext.newRecord.getValue({
fieldId: 'custbody_cd12_approval_status'
});
var currentUser = runtime.getCurrentUser().id;
if((commission_status == 3) && (pricelevelcheckbox == true)){
CustRec.removeButton('approve');
CustRec.removeButton('edit');
}
// When change in process level edit and remove button removed , except for VP
if((pricelevelcheckbox == true) && (currentUser != (-5))){
CustRec.removeButton('approve');
CustRec.removeButton('edit');
}
if(scriptContext.type == 'edit'){
var html = "<script>jQuery( document ).ready(function() { jQuery('#custbody_cd12_sales_rep_comm_percent').prop('disabled', true); });</script>";
//disabling the sub tabs if the status is order lost or cancel and Not Administrator Role.
var jobField2 = CustRec.addField({
id:'custpage_spfid',
type: 'INLINEHTML',
label: 'spf Id'
});
jobField2.defaultValue=html;
}
// Edit and romove button visible for VP
if((currentUser == (-5)) && (pricelevelcheckbox == true) && (commission_status == 1)){
if(scriptContext.type == 'view'){
CustRec.removeButton('approve');
CustRec.clientScriptFileId = 4953;
//Setting the button to approve commsiion
var cancelSB=CustRec.addButton({
id:'custpage_vp_approve_btn',
label:'Commission Approve',
functionName:'salesorderapproval'
});
}
if(scriptContext.type == 'edit'){
var html = "<script>jQuery( document ).ready(function() { jQuery('#custbody_cd12_sales_rep_comm_percent').prop('disabled', false); });</script>";
jobField2.defaultValue=html;
}
}
}catch (e) {
logme("error@beforeLoad", getError(e));
}
}
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type
* @Since 2015.2
*/
function afterSubmit(scriptContext) {
try{
var count =0;
var SO_id = scriptContext.newRecord;
var sales_id = scriptContext.oldRecord;
logme("SO_id",SO_id);
//Commission approval status
var approval_status = scriptContext.newRecord.getValue({
fieldId: 'custbody_cd12_approval_status'
});
//If VP approved SO , no need for approval process
if(approval_status == 4){
var cust_id = scriptContext.newRecord.getValue({
fieldId: 'entity'
});
var approval_status = scriptContext.newRecord.getValue({
fieldId: 'custbody_cd12_approval_status'
});
var cust_pricelevel = search.lookupFields({
type: search.Type.CUSTOMER,
id: cust_id,
columns: ['pricelevel']
});
var pricelevel = cust_pricelevel.pricelevel[0].value;
var price_level = cust_pricelevel.pricelevel[0].text;
if((approval_status != 1) || (approval_status != 2)){
var numLines = scriptContext.newRecord.getLineCount({
sublistId: 'item'
});
for(i=0;i<numLines;i++){
var applyvalue = scriptContext.newRecord.getSublistValue({
sublistId: 'item',
fieldId: 'price',
line: i
});
var sublistvaluetype = scriptContext.newRecord.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
if(sublistvaluetype == 'InvtPart'){
if(applyvalue != pricelevel){
var pricelevel_requested = scriptContext.newRecord.getSublistValue({
sublistId: 'item',
fieldId: 'price_display',
line: i
});
count = count+1;
}
}
}
if(count > 0){
approvalprocess(SO_id,price_level,pricelevel_requested);
}else{
record.submitFields({
type: record.Type.SALES_ORDER,
id: SO_id.id,
values: {
'custbody_cd12_jj_price_level_change': false
}
});
}
}
}
}catch (e) {
logme("error@afterSubmit", getError(e));
}
}
/*******************************************************************************
* Approval process : Function to send mail to sales rep
*
* @param e
* @returns
*
* Created on 14-May-2019 by Maria
*/
function approvalprocess(SO_id,price_level,pricelevel_requested){
try{
var SO_status = SO_id.getValue({
fieldId: 'orderstatus'
});
var SO_salesrep = SO_id.getValue({
fieldId: 'salesrep'
});
//current Sales order ID
var Sales_id = SO_id.id;
var cust_name = SO_id.getValue({
fieldId: 'entity'
});
var customer_name = search.lookupFields({
type: search.Type.CUSTOMER,
id: cust_name,
columns: ['entityid']
});
var customer = customer_name.entityid;
logme("customer",customer);
var so_po = SO_id.getValue({
fieldId: 'otherrefnum'
});
var sales_so = search.lookupFields({
type: search.Type.SALES_ORDER,
id: Sales_id,
columns: ['tranid']
});
var sales_order = sales_so.tranid;
var amount = SO_id.getValue({
fieldId: 'total'
});
var cust_privelevel = price_level;
var req_pricelevel = pricelevel_requested;
logme("Sales_id",Sales_id);
//Set the field value for price level to true and status to pending approval.
record.submitFields({
type: record.Type.SALES_ORDER,
id: Sales_id,
values: {
'custbody_cd12_jj_price_level_change': true,
'orderstatus':'A',
'custbody_cd12_approval_status':3
}
});
var email_temp = record.load({
type: record.Type.EMAIL_TEMPLATE,
id: 17,
isDynamic: true,
});
var content = email_temp.getValue({
fieldId: 'content'
});
logme("content", content);
var renderer = render.create();
//logme("SO_id",SO_id);
var myContent = renderer.addRecord({
templateName: 'transaction',
record: record.load({
type: record.Type.SALES_ORDER,
id: SO_id.id
})
});
renderer.templateContent = content;
var Emailcontent = renderer.renderAsString();
var emplid = -5;
email.send({
author: emplid,
recipients: 'marie@corpdesign.com', //Sales rep
subject: 'Approval for additional discount required-'+sales_order,
body: '<br>' + 'Sales order need approval for commission: As per your agreement with Corp Design, this approval process may affect your compensation, please review carefully prior approving. Time is of the essence as the order wont be process prior your approval is received.'+ '<br>'+
'Sales Order Details :' + '<br>' + '<br>' +
'Clients name :' + customer +'<br>' +
'Clients PO# :' + so_po + '<br>' +
'SO # :' + sales_order + '<br>' +
'Amount :' + amount + '<br>' +
'Default price level :' + cust_privelevel + '<br>' +
'Price Level requested :' + req_pricelevel + '<br>' +
Emailcontent
});
email.send({
author: emplid,
recipients: 'martin@corpdesign.com', //VP
subject: 'Discounting Approval Request-' +sales_order,
body: '<br>' + 'Order requires your approval before it can be processed' + '<br>'+
'Sales Order Details :' + '<br>' + '<br>' +
'Clients name :' + customer +'<br>' +
'Clients PO# :' + so_po + '<br>' +
'SO # :' + sales_order + '<br>' +
'Amount :' + amount + '<br>' +
'Default price level :' + cust_privelevel + '<br>' +
'Price Level requested :' + req_pricelevel
});
}catch (e) {
logme("error@approvalprocess", getError(e));
}
}
/*******************************************************************************
* return error
*
* @param e
* @returns
*
* Created on 14-May-2019 by Maria
*/
function getError(e) {
var stErrMsg = '';
if (e.getDetails != undefined) {
stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
+ e.getStackTrace();
} else {
stErrMsg = '_' + e.toString();
}
return stErrMsg;
}
/*******************************************************************************
* Log these data
*
* @param title
* @param details
* @returns
*
* Created on 14-May-2019 by Maria
*/
function logme(title, details) {
log.debug({
title : title,
details : details
});
}
return {
beforeLoad: beforeLoad,
//beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
};
});
Suitelet:-
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
/**
* Script Description
* In SO, when Sales rep approved mail is send to the VP
.
*
*/
/*******************************************************************************
* CORP DESIGN
* **************************************************************************
*
* Date: 23/05/2019
*
* Author: Jobin & Jismi IT Services LLP
*
*
* REVISION HISTORY
*
* Revision $ 23/05/2019 Maria: Create
*
******************************************************************************/
define(['N/http', 'N/record', 'N/redirect', 'N/search', 'N/ui/serverWidget', 'N/redirect', 'N/render', 'N/email'],
function(http, record, redirect, search, serverWidget, redirect, render, email) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object} context
* @param {ServerRequest} context.request - Encapsulation of the incoming request
* @param {ServerResponse} context.response - Encapsulation of the Suitelet response
* @Since 2015.2
*/
function onRequest(context) {
try{
var approve = context.request.parameters.approve;
//logme("approve", approve);
var so_id = context.request.parameters.so_id;
//logme("so_id", so_id);
var salesorder = record.load({
type: record.Type.SALES_ORDER,
id: so_id,
isDynamic: true,
});
var status = salesorder.getValue({
fieldId: 'custbody_cd12_approval_status'
});
var pricelevelcheckbox = salesorder.getValue({
fieldId: 'custbody_cd12_jj_price_level_change'
});
if((status == '2') || (status == '1')){
context.response.write('You have already approved this sales order.');
}else{
var id = record.submitFields({
type: record.Type.SALES_ORDER,
id: so_id,
values: {
'custbody_cd12_approval_status': 1
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
context.response.write('Approved. Thank you.The order is under next level of approval process.');
log.debug('test123',serverWidget.FieldDisplayType.NORMAL);
var Sales_id = record.load({
type: record.Type.SALES_ORDER,
id: so_id,
isDynamic: true,
});
var cust_name = Sales_id.getValue({
fieldId: 'entity'
});
var customer_name = search.lookupFields({
type: search.Type.CUSTOMER,
id: cust_name,
columns: ['entityid']
});
var customer = customer_name.entityid;
var so_po = Sales_id.getValue({
fieldId: 'otherrefnum'
});
var sales_order = Sales_id.getValue({
fieldId: 'tranid'
});
/*var sales_so = search.lookupFields({
type: search.Type.SALES_ORDER,
id: Sales_id,
columns: ['tranid']
});*/
//var sales_order = sales_so.tranid;
var amount = Sales_id.getValue({
fieldId: 'total'
});
var emplid = -5;
email.send({
author: emplid,
recipients: 'martin@corpdesign.com', //VP
subject: 'Discounting Approval Request-' + sales_order ,
body: '<br>'+ 'Order requires your approval before it can be processed. The order is approved by sales rep.'+ '<br>'+
'Sales Order Details :' + '<br>' + '<br>' +
'Clients name :' + customer +'<br>' +
'Clients PO# :' + so_po + '<br>' +
'SO # :' + sales_order + '<br>' +
'Amount :' + amount + '<br>'
});
}
}catch (e) {
logme("error@onRequest", getError(e));
}
}
/*******************************************************************************
* return error
*
* @param e
* @returns
*
* Created on 14-May-2019 by Maria
*/
function getError(e) {
var stErrMsg = '';
if (e.getDetails != undefined) {
stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
+ e.getStackTrace();
} else {
stErrMsg = '_' + e.toString();
}
return stErrMsg;
}
/*******************************************************************************
* Log these data
*
* @param title
* @param details
* @returns
*
* Created on 14-May-2019 by Maria
*/
function logme(title, details) {
log.debug({
title : title,
details : details
});
}
return {
onRequest: onRequest
};
});
Client Script:-
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/**
* Script Description
* In SO, Added a button for commission approval.
.
*
*/
/*******************************************************************************
* CORP DESIGN
* **************************************************************************
*
* Date: 23/05/2019
*
* Author: Jobin & Jismi IT Services LLP
*
*
* REVISION HISTORY
*
* Revision $ 23/05/2019 Maria: Create
*
******************************************************************************/
define(['N/currentRecord','N/email','N/runtime','N/record','N/search','N/url','N/https'],
function(currentRecord,email,runtime,record,search,url,https) {
/**
* 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 salesorderapproval(){
try{
var currentRec = currentRecord.get();
var recid = currentRec.id;
console.log("recid",recid);
var salesorder = record.load({
type: record.Type.SALES_ORDER,
id: recid,
isDynamic: true,
});
var commission_so_percent = salesorder.getValue({
fieldId: 'custbody_cd12_sales_rep_comm_percent'
});
console.log("commission_so_percent",commission_so_percent);
var commission_percent = salesorder.getValue({
fieldId: 'custbody_cd12_so_commission'
});
console.log("commission_percent",commission_percent);
if(commission_so_percent == commission_percent){
var message = confirm("You have not changed the commission percent.Do you still want to approve the commission?");
if(message == true){
var id = record.submitFields({
type: record.Type.SALES_ORDER,
id: recid,
values: {
'custbody_cd12_approval_status': 2,
'custbody_cd12_jj_price_level_change':false,
'orderstatus': 'B'
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
window.location.reload();
}
}else{
var id = record.submitFields({
type: record.Type.SALES_ORDER,
id: recid,
values: {
'custbody_cd12_approval_status': 2,
'custbody_cd12_jj_price_level_change':false,
'orderstatus': 'B'
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
window.location.reload();
}
}catch (e) {
logme("error@beforeLoad", getError(e));
}
}
/*******************************************************************************
* return error
*
* @param e
* @returns
*
* Created on 14-May-2019 by Maria
*/
function getError(e) {
var stErrMsg = '';
if (e.getDetails != undefined) {
stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
+ e.getStackTrace();
} else {
stErrMsg = '_' + e.toString();
}
return stErrMsg;
}
/*******************************************************************************
* Log these data
*
* @param title
* @param details
* @returns
*
* Created on 14-May-2019 by Maria
*/
function logme(title, details) {
log.debug({
title : title,
details : details
});
}
return {
pageInit: pageInit,
salesorderapproval: salesorderapproval
};
});