Suitelet to return the customer voucher numbers statistics (booked, unbooked, and completed) to the lab home on the SCA website.
Suitelet
/****************************************************************************
* Name: Get Customer Voucher Numbers (getCustomerVoucherNumbers.js)
*
* Script Type: Suitelet
*
* Client: Enlighten Smiles
*
* Version: 1.0.0 - 04/05/2016 - initial release - TM
* 1.1.0 - 08/06/2016 - As per Sanjay's request in Case S11039, the count which is shown for in progress vouchers in the lab section of the website
* is now the sum of the amount of at lab, processing for dispatch and processing for dispatch 2 vouchers,
* rather than the booked voucher count - TM
* 1.2.0 - 27/07/2016 - Changed the in progress and completed vouchers search filters to be retrieved from a
* deployment parameter to match the actual case list shown in the SCA site - TM
* 1.2.1 - 11/09/2018 - S18995 Count un-booked vouchers - SB
*
* Author: FHL
*
* Purpose: Suitelet to return the customer voucher numbers statistics (booked, unbooked and completed) to the lab home in the SCA website
*
* Script: customscript_getcustomervouchernumbers
* Deploy: customdeploy_getcustomervouchernumbers
*
* Notes:
*
* Library: Library.js
* SCA Denali - Enlighten Smiles/Modules/es-modules/ES.Lab@es
*
******************************************************************************/
//1.2.0 added and removed others
var inProgressCasesFilters = null;
var completedCasesFilters = null;
var unbookedStatus = null; // 1.2.1
var voucherPrefixes = ''; // 1.2.1
/**
* @method getCustomerVoucherNumbers
* @description Entry function
* @param {nlobjRequest} request Request object
* @param {nlobjResponse} response Response object
* @returns {Void} Any output is written via response object
*/
function getCustomerVoucherNumbers(request, response)
{
var voucherStats = {};
var voucherStatsStr = "";
var jsonpCallback = null;
try
{
initialise(); //1.1.0
jsonpCallback = request.getParameter('callback');
voucherStats = getVoucherStats(request);
voucherStatsStr = JSON.stringify(voucherStats);
if(jsonpCallback)
{
response.write(jsonpCallback + '(' + voucherStatsStr +')');
}
else
{
response.write(voucherStatsStr);
}
}
catch(e)
{
errorHandler("getCustomerVoucherNumbers", e);
}
}
/**
* @method initialise
* @description Function to initialise deployment parameter values
* 1.2.0 - removed deployment parameters which get voucher status values as it is no longer needed
* added code to get the in progress and completed case filters from a deployment parameter
* 1.2.1 Count un-booked vouchers
* @since 1.1.0
*/
function initialise()
{
var category = null;
var paramRecordID = null; //1.2.0
var paramLongFieldValue = null; //1.2.0
try
{
category = getParameterCategoryPrefix();
//1.2.0 Code added to get filters (gets record id and looks up long value field)
paramRecordID = genericSearchTwoParams('customrecord_deploymentparameters', 'custrecord_parametercategory', category + ' Saved Searches', 'name', 'In Progress Cases Filters');
paramLongFieldValue = nlapiLookupField("customrecord_deploymentparameters", paramRecordID, 'custrecord_deploymentparameterlong');
inProgressCasesFilters = parseFilters(paramLongFieldValue);
paramRecordID = genericSearchTwoParams('customrecord_deploymentparameters', 'custrecord_parametercategory', category + ' Saved Searches', 'name', 'Completed Cases Filters');
paramLongFieldValue = nlapiLookupField("customrecord_deploymentparameters", paramRecordID, 'custrecord_deploymentparameterlong');
completedCasesFilters = parseFilters(paramLongFieldValue);
// 1.2.1
unbookedStatus = genericSearchTwoParams('customrecord_deploymentparameters', 'custrecord_parametercategory', category + ' Status', 'custrecord_scakey', 'UNBOOKED_STATUS');
unbookedStatus = nlapiLookupField("customrecord_deploymentparameters", unbookedStatus, 'custrecord_deploymentparametervalue'); // 1
voucherPrefixes = genericSearchTwoParams('customrecord_deploymentparameters', 'custrecord_parametercategory', category + ' SCA', 'custrecord_scakey', 'VOUCHER_PREFIXES');
voucherPrefixes = nlapiLookupField("customrecord_deploymentparameters", voucherPrefixes, 'custrecord_deploymentparametervalue'); // '2,3,4'
if (voucherPrefixes)
{
voucherPrefixes = voucherPrefixes.split(','); // [2, 3, 4]
}
}
catch(e)
{
errorHandler("initialise", e);
}
}
/**
* @method parseFilters
* @description Function to parse filters which come from deployment parameters so that errors related to parsing can be caught and logged appropriately
* @since 1.2.0
* @param {String} filterParam - Filter parameters.
* @return {Array} filters
*/
function parseFilters(filterParam)
{
var filters = [];
try
{
filters = JSON.parse(filterParam); //Parse to array as it is a string when pulled from the record field
}
catch(e)
{
errorHandler("parseFilters", e);
}
return filters;
}
/**
* @method getVoucherStats
* @description This function loads the customer record using the internal id passed into the suitelet, makes the call to get the voucher stats and returns them to the entry function
* 1.1.0 - Retrieve voucherStats.bookedVouchers from
* 1.2.0 - Changed the completed voucher count to be retrieved from a search rather than the completed vouchers field on the customer record. The search is handled in the
* getVoucherCount function.
* 1.2.1 Count un-booked vouchers
* @param {nlobjRequest} request - HTTP request.
* @returns {Object} voucherStats
*/
function getVoucherStats(request)
{
var customerId = null;
var voucherStats = {};
var customerRecord = null;
var filters = []; // 1.2.1
var columns = []; // 1.2.1
var searchResults = null; // 1.2.1
try
{
customerId = request.getParameter('customerId');
voucherStats.error = false;
if(customerId !== null)
{
customerRecord = nlapiLoadRecord('customer', customerId);
if(customerRecord !== null)
{
// 1.2.1 Search for un-booked vouchers
//Add filter for the customer
filters.push(new nlobjSearchFilter('custrecord_cust_customer', null, 'anyof', customerId));
//Only return unbooked vouchers
filters.push(new nlobjSearchFilter('custrecord_cust_vou_status', null, 'anyof', unbookedStatus)); //1.3.0
// Return vouchers where product prefix is in voucherPrefixes
filters.push(new nlobjSearchFilter('custrecord_product_prefix', null, 'anyof', voucherPrefixes)); //1.3.0
filters.push(new nlobjSearchFilter('custrecord_cust_reimp', null, 'is', 'F'));
columns.push(new nlobjSearchColumn('internalid', null, 'COUNT'));
searchResults = nlapiSearchRecord('customrecord_voucher', null, filters, columns);
if (searchResults)
{
// There should only be 1 result
voucherStats.unbookedVouchers = searchResults[0].getValue('internalid', null, 'COUNT');
}
voucherStats.inProgressVouchers = getVoucherCount(customerId, 'inprogress'); //1.1.0
voucherStats.completedVouchers = getVoucherCount(customerId, 'completed'); //1.2.0
}
else
{
voucherStats.noRecord = 'No Customer Record was found with the provided ID.';
voucherStats.error = true;
}
}
else
{
voucherStats.noId = 'No customer ID has been provided.';
voucherStats.error = true;
}
}
catch(e)
{
errorHandler("getVoucherStats", e);
voucherStats.errorCode = e.code;
voucherStats.error = true;
}
return voucherStats;
}
/**
* @method getVoucherCount
* @description Function to get the in progress voucher count, which consists of vouchers with either the at lab, processing for dispatch 1 or processing for dispatch 2 statuses
* 1.2.0 - changed the way in which the filters for the search are created. Instead of hard-coding the array, the array of filters is retrieved from a deployment parameter,
* and then the filter for the customer is appended afterwards.
* Also changed this function so that it is reused for in progress and completed vouchers.
* @added 1.1.0
* @param {Number} customerId - Customer's internalid.
* @param {String} caseType - Case type.
* @returns {Number} voucherCount
*/
function getVoucherCount(customerId, caseType)
{
var filters = [];
var columns = [];
var result = null;
var voucherCount = 0;
try
{
//1.2.0
if(caseType === 'inprogress')
{
filters = inProgressCasesFilters;
}
else if(caseType === 'completed')
{
filters = completedCasesFilters;
}
filters.push("and");
filters.push(['custrecord_cust_customer', 'anyof', customerId]);
columns =
[
new nlobjSearchColumn('name')
];
result = nlapiSearchRecord('customrecord_voucher', null, filters, columns);
if(result != null)
{
voucherCount = result.length;
}
}
catch (e)
{
errorHandler("getInProgressVouchers", e);
}
return voucherCount;
}