Jira code:MR-132
The client has created a KPI for Tracfone Activations which compares Activations on This Month vs. Same Month Last Financial Year
clicking on the trend icon(~). Which will show the trend graph in different styles using native methods, we are not able to give a minimum or maximum value for the KPI. Also, I tried to bring This month data in the Trend graph. But I could not. Create a script to show this KPI.
Portlet script
/**
* @NApiVersion 2.x
* @NScriptType Portlet
* @NModuleScope SameAccount
*/
/*******************************************************************************
* CLIENTNAME:MEGATEL
* MR 132 -Custom KPI to compare custom record values
*************************************************************************
* Date : 21/01/2019
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : To create a KPI Graph for Tracfone Activation record
*
* Date created :21/01/2019
*
* REVISION HISTORY
*
* Revision 1.0 ${21/01/2019} aj : created
* 1.1 ${24/01/2019} aj MR 134 To add different criteria for regional manager
*
*
******************************************************************************/
define(
[ 'N/record', 'N/search',
'Jobin & Jismi IT Services LLP/MR 132/loader', 'N/runtime' ],
/**
* @param {record} record
*/
function(record, search, loader, runtime) {
/**
* Definition of the Portlet script trigger point.
*
* @param {Object} params
* @param {Portlet} params.portlet - The portlet object used for rendering
* @param {number} params.column - Specifies whether portlet is placed in left (1), center (2) or right (3) column of the dashboard
* @param {string} params.entity - (For custom portlets only) references the customer ID for the selected customer
* @Since 2015.2
*/
function render(params) {
try {
var portlet = params.portlet;
portlet.title = 'Tracfone Activation KPI ';
var userObj = runtime.getCurrentUser();
var userId = userObj.id;
// to create graph
// to get the Count of ID's
var getCountOfThisMonth = calculateID(1, userId);
var getCountOfLastYear = calculateID(0, userId);
var defaultMinimum = parseFloat(getCountOfLastYear * 0.20)
+ parseFloat(getCountOfLastYear);
var graphField = portlet.addField({
id : '_graph',
type : 'inlinehtml',
label : 'Graph'
});
// for graph
var graph = '<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>'
+ '<div id="chart_div" style="width:100%;height:80%"></div>'
+ '<script>google.charts.load(\'current\', {packages: [\'corechart\', \'bar\']});'
+ ' google.charts.setOnLoadCallback(drawBasic);'
+ ''
+ ' function drawBasic() {'
+ ''
+ ' '
+ ' var data = new google.visualization.DataTable();'
+ ' data.addColumn(\'string\', \'Months\');'
+ ' data.addColumn(\'number\', \'This month count\');'
+ ' data.addColumn(\'number\', \'Same Month last year count\');'
+ ' data.addColumn(\'number\', \'Target Count\');'
+ ' data.addRows(['+ '[{v: \'Months\', f: \'Months\'}, '+ getCountOfThisMonth+','+getCountOfLastYear+','+defaultMinimum +']'
+ ' '
+ ' ]);'
+ ''
+ ' var options = {'
+ ' title: \'Tracfone activation \','
+ ' hAxis: {'
+ ' title: \'Month\''
+ ' '
+ ' },'
+ ' vAxis: {'
+ ' title: \'Count\''
+ ' }'
+ ' };'
+ ''
+ ' var chart = new google.visualization.ColumnChart('
+ ' document.getElementById(\'chart_div\'));'
+ ''
+ ' chart.draw(data, options);'
+ ' }</script>';
graphField.defaultValue = graph;
graphField.updateLayoutType({
layoutType : 'normal'
});
graphField.updateBreakType({
breakType : 'startrow'
});
var test = portlet.addField({
id : 'test',
type : 'inlinehtml',
label : null
});
test.updateLayoutType({
layoutType : 'normal'
});
var myvar = '<html lang="en">'
+ '<head>'
+ ' <meta charset="utf-8">'
+ ' <meta name="viewport" content="width=device-width, initial-scale=1">'
+ ' <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">'
+ ' <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>'
+ ' <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>'
+ '</head>' + '<body>'
+ '<div class="after-class">' + ' <h2></h2>'
+ ' ' + ' ' + '</div>' + '' + '</body>';
test.defaultValue = myvar;
test.padding = 10;
test.updateBreakType({
breakType : 'startrow'
});
} catch (e) {
//log.debug("Err@ FN ",e.message);
log.error("Err@ onAction FN ", e.message);
}
}
/***************
* To get the Count of ID within this month & Last year
*/
function calculateID(isThisMonth, userId) {
try {
var filterArray = [];
if (isThisMonth == 1) {
filterArray.push([ "custrecord_jj_tra_activation_date",
"within", "thismonth" ]);
} else {
filterArray.push([ "custrecord_jj_tra_activation_date",
"within", "samemonthlastfiscalyear" ]);
}
if (userId == 14055) {
log.debug({
title : 'user',
details : 'testuser'
});
filterArray.push("AND", [ "custrecord_jj_sales_rep_tracfone", "anyof", "8047" ]); //rose userId
}else {
filterArray.push("AND", [ "custrecord_jj_sales_rep_tracfone", "anyof", userId ]);
}
// to get the Count of ID's
var customrecord_jj_tracfone_activationsSearchObj = search
.create({
type : "customrecord_jj_tracfone_activations",
filters : filterArray,
columns : [ search.createColumn({
name : "internalid",
summary : "COUNT",
label : "Internal ID"
}) ]
});
var searchResultCount = customrecord_jj_tracfone_activationsSearchObj
.runPaged().count;
log
.debug(
"customrecord_jj_tracfone_activationsSearchObj result count",
searchResultCount);
var Count;
if (searchResultCount > 0) {
var searchResult = customrecord_jj_tracfone_activationsSearchObj
.run().getRange({
start : 0,
end : 1
});
Count = searchResult[0].getValue({
name : "internalid",
summary : "COUNT",
label : "Internal ID"
});
}
return Count;
} catch (e) {
//log.debug("Err@ FN calculateID",e.message);
log.error("Err@ onAction FN ", e.message);
}
}
return {
render : render
};
});