Skip to content

Instantly share code, notes, and snippets.

@23maverick23
Created February 20, 2014 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 23maverick23/9125709 to your computer and use it in GitHub Desktop.
Save 23maverick23/9125709 to your computer and use it in GitHub Desktop.
NSOA: Update project.budget_time using budget transactions
/**
* Update the project total budget hours using
* the hours field on project budget transactions.
*
* Version Date Author Remarks
* 1.00 20 Feb 2014 Ryan Morrissey
*
*/
function updateProjectBudgetHours(type) {
try {
// DEBUG: Uncomment next line to enable XML logging
// var wsLog = NSOA.wsapi.enableLog(true);
// list all fields used in script
var FLD_PRJ_ID = 'id',
FLD_PRJ_BUD_HRS = 'budget_time',
FLD_BUD_PID = 'projectid';
// store newly saved budget record
var updBudget = NSOA.form.getNewRecord();
// create new budget object to store information
var budRec = new NSOA.record.oaBudget();
budRec.projectid = updBudget.projectid;
var budRequest = {
type : "Budget",
method : "equal to",
fields : "id,budget_hours__c", // budget_hours__c is a custom hours field
attributes : [
{
name : "limit",
value : "100"
}
],
objects : [budRec]
};
// disable the current user's filter set while script runs
NSOA.wsapi.disableFilterSet(true);
// search for all budget transactions with current projectid
var budResults = NSOA.wsapi.read(budRequest);
if (!budResults || !budResults[0]) {
NSOA.meta.log('error', 'Unexpected error! Could not return project budgets.');
return;
} else if (budResults[0].errors !== null && budResults[0].errors.length > 0) {
prjResults[0].errors.forEach(function(err) {
var fullError = err.code + ' - ' + err.comment + ' ' + error.text;
NSOA.meta.log('error', 'Error: ' + fullError);
});
return;
}
var b,
totalBudHrs = 0,
budObj = budResults[0].objects,
budObjLen = budObj.length;
for (b = 0; b < budObjLen; b++) {
var budHrs = parseInt(budObj[b].budget_hours__c, 10);
totalBudHrs += budHrs; // add all hours together
}
// create new project object to store information
var prjRec = new NSOA.record.oaProject();
prjRec[FLD_PRJ_ID] = updBudget[FLD_BUD_PID];
prjRec[FLD_PRJ_BUD_HRS] = totalBudHrs;
// disable the current user's filter set while script runs
NSOA.wsapi.disableFilterSet(true);
// update the project budget
var prjResults = NSOA.wsapi.modify(
[],
[prjRec]
);
if (!prjResults || !prjResults[0]) {
NSOA.meta.log('error', 'Unexpected error! Project was not updated.');
} else if (prjResults[0].errors !== null && prjResults[0].errors.length > 0) {
prjResults[0].errors.forEach(function(err) {
var fullError = err.code + ' - ' + err.comment + ' ' + error.text;
NSOA.meta.log('error', 'Error: ' + fullError);
});
return;
}
} catch(e) {
NSOA.meta.log('error', 'Try/catch error: ' + e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment