Skip to content

Instantly share code, notes, and snippets.

@nabil-nuvolo
Last active April 27, 2019 00:02
Show Gist options
  • Save nabil-nuvolo/fee07c269434175968609b299c4179c8 to your computer and use it in GitHub Desktop.
Save nabil-nuvolo/fee07c269434175968609b299c4179c8 to your computer and use it in GitHub Desktop.
STRY0051147_peer_review
// Old
function getField(type) {
if (type === 'asset') {
return 'asset';
}
if (type === 'space') {
return 'work_location';
}
}
// Refactored
function getField(type) {
var field_map = {
asset: "asset",
space: "work_location"
};
return field_map[type] || "asset";
}
function getWorkOrderMap(type, arr) {
var np = new x_nuvo_mobile.NuvoloProperties();
var limit = np.getProperty(
'related_work_orders_limit',
'EAM Service Portal'
);
var len = arr.length;
var woGR;
// NME: initialize display value var in hopes of finding it later
var map = {};
for (var i = 0; i < len; i++) {
// Set this at the beginning of the for loop since its getting reset on each loop anyway
var display_value = "";
//skip blank entries
if (arr[i] === '') {
continue;
}
woGR = new GlideRecord('x_nuvo_eam_work_order');
woGR.addQuery('active', 'true');
woGR.addQuery(getField(type), arr[i]);
//not closed
woGR.addEncodedQuery('stateIN-5,1,100,110,2,120,130,140');
//created in the last 30 days
woGR.addEncodedQuery('sys_created_onRELATIVEGE@dayofweek@ago@30');
//fault related
woGR.addEncodedQuery('work_order_type.fault_related=true');
woGR.orderByDesc('sys_created_on');
//configurable limit
woGR.setLimit(limit);
woGR.query();
// NME: Break early to avoid further code execution
if (woGR.getRowCount() == 0) continue;
var results = [];
while (woGR.next()) {
//build and push work order object
results.push({
number: woGR.getValue('number'),
short_description: woGR.getValue('short_description'),
description: shorten(woGR.getValue('description')),
sys_id: woGR.getValue('sys_id'),
sys_created_on: woGR.getValue('sys_created_on')
});
// NME: Updates display_value only when it returns a value
display_value = woGR.getDisplayValue(getField(type)) || display_value;
}
// Eliminated entire if & nested if blocks
map[arr[i]] = {
workOrders: results,
display_value: display_value || arr[i]
};
}
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment