Wednesday, April 17, 2019

Script > Journal Entry Approval Routing

Below is sample code for approval routing for Journal entries. This sample code requires a custom field to get the current user that is creating the journal entry. It then sets the field value on PageInit function. The next function will set the approved checkbox to false (unchecked). This script will stop users from approving their own journal entries. To get a list of roles go to the following location: Setup > Users/Roles > Manage Roles. Please note this is a client side script.

// checks the user and sets the approved checkbox by default
function getUserPageInit() {
var context = nlapiGetContext();
var user = context.getUser();
nlapiSetFieldValue('custbody1', user);
nlapiSetFieldValue('approved', 'F');
}

// Checks the current role of the user, if the condition is satisfied the user will be allowed to approve their own Journal Entries otherwise the user cannot approve his own journal entries.
function approveJournalEntry() {
var context = nlapiGetContext();
var user = context.getUser();
var createdBy = nlapiGetFieldValue('custbody1');
var approve = nlapiGetFieldValue('approved');
var userRole = context.getRole();
var role = false;

var approvedRoles = new Array();
approvedRoles[0] = 3;
approvedRoles[1] = 9;
approvedRoles[2] = 26;

for (var i = 0; i < approvedRoles.length; i++) {
if (approvedRoles[i] == userRole) {
role = true;
}
}

if (role != true) {
if (user == createdBy && approve == 'T') {
alert('Journal entry needs to be approved by independent approver');
nlapiSetFieldValue('approved', 'F');
return false;
} else {
return true;
}
}
return true;
}

*This is for SuiteScript version 1.0

1 comment:

  1. i got error while save the journal entry.The error is "Please enter value(s) for: Approver".Can you please help me out this?

    ReplyDelete