Last active
April 4, 2018 15:33
-
-
Save 23maverick23/d5f243c7fd36a795a0c147c0f8eaa430 to your computer and use it in GitHub Desktop.
NSOA: Calculate project budget bill rate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Main entrance function (afterSave) | |
*/ | |
function main(type) { | |
var DECIMALS = 2; | |
var FLD_RATE = 'budget_rate__c'; | |
var needsUpdate = false; | |
var newRec = NSOA.form.getNewRecord(); | |
if (type == "create") { | |
needsUpdate = true; | |
} else if (type == "edit") { | |
var oldRec = NSOA.form.getOldRecord(); | |
if (oldRec.budget_time !== newRec.budget_time || oldRec.budget !== newRec.budget) { | |
needsUpdate = true; | |
} | |
} | |
if (!needsUpdate || needsUpdate === false) { | |
return; | |
} | |
var budget = parseFloat(newRec.budget) || 0.00; | |
var time = parseFloat(newRec.budget_time) || 0.00; | |
if (!budget || budget === 0 || budget === 0.00) { | |
return; | |
} | |
if (!time || time === 0 || time === 0.00) { | |
return; | |
} | |
var rate = round(budget / time, DECIMALS); | |
var prjRec = new NSOA.record.oaProject(); | |
prjRec.id = newRec.id; | |
prjRec[FLD_RATE] = rate; | |
NSOA.wsapi.disableFilterSet(true); | |
var attr = {name: "update_custom", value: "1"}; | |
var results = NSOA.wsapi.modify([attr], [prjRec]); | |
} | |
/** | |
* Round a number based on decimal points | |
*/ | |
function round(number, decimals) { | |
number = parseFloat(number); | |
decimals = parseInt(decimals) || DECIMALS; | |
if (isNaN(number) || isNaN(decimals)) { | |
return number; | |
} | |
return (Math.round(number * Math.pow(10, 2)) / Math.pow(10, 2)).toFixed(decimals); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment