// // Integrity Manager PRE-Event Trigger //

// Update timesheet item's time entries based on field changes in a related Task Item. //

// // @param Field:relationship Timesheet_Field_Name // Name of the relationship field that is used to link a (public) Task to a (private) Timesheet. // Users without TimeSheet permisison should not be able to see this field. // // @param Field:float Duration_Field_Name // Name of the float field in hte Task that contains the daily spent effort (max 24.0) which is // to be set as a time entry in the related TimeSheet for the current user. // Users without TimeSheet permisison should not be able to see this field. // // For debugging purposes function print(s) { java.lang.System.out.println(s); } function abort(s) { eb.abortScript(s, true); } function noPost(s) { eb.abortScript(s, false); } var eb = bsf.lookupBean("siEnvironmentBean"); var sb = bsf.lookupBean("imServerBean"); var params = bsf.lookupBean("parametersBean"); var delta = bsf.lookupBean("imIssueDeltaBean"); eb.setMessageCategory("DEBUG"); logging = eb.messageCheck(); debugging = eb.messageCheck(10); if (eb.getEventName() != "Issue.changed.pre"){ abort(eb.getMessage("ERROR_SCRIPT_CONDITION")); } if (debugging) { eb.print("---------------begin---------------"); } var relationshipField = params.getParameter("Timesheet_Field_Name"); var durationField = params.getParameter("Duration_Field_Name"); // Check if we have the linked issue var relatedTimesheets; if (relationshipField != null && relationshipField.length() > 0){ relatedTimesheets = delta.getRelatedIssues(relationshipField); } else{ relatedTimesheets = delta.getRelatedIssues(); } // store current user for later use var currentUser = delta.getCurrentUser(); // get the value to set from the delta var duration = delta.getNewFieldValue(durationField) // create a date (utc midnight) reflecting the value of today and the environment's timezone offset. var now = new Date(); var currentDate = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()); currentDate.setHours(0,0,0,0); // eliminate the time //valid format for the API call is "MMM d, yyyy" var options = { month: 'short', day: 'numeric' }; var currentDateStr = currentDate.toLocaleDateString('en-EN', options); // use StringBuilder to create AIP command parameter var actionSB = new java.lang.StringBuilder(); actionSB.append(""); // Check each relatedTimesheets issue (should be only one) for (var i = 0; i < relatedTimesheets.length; i++) { var timesheet = sb.getIssueDeltaBean(relatedTimesheets[i]); var issueID = timesheet.getIssueID().toString() ; // int // first try to find an entry that matches user and date, so we can decide whether to Create or Edit an entry var entryexists = 0; // get all existing time entries var tebs = timesheet.getTimeEntryBeans();//returns LocalTriggerManager.ScriptTimeEntryBean[] for (var tt = 0; (tt < tebs.length) && (entryexists === 0); tt++) { // iterate all existing entries var entryuser = tebs[tt].getUserName(); // String var entryDate = new Date(tebs[tt].getEntryDate()); // convert java.util.Date into JavaScript Date /* TODO: use already given Duration for "add time" instead of "replace time" var entryDuration = tebs[tt].getDuration(); // float */ // compare the User Strings var isUserEqual = currentUser === entryuser; // compare the Dates (if they a equal, the diff must be 0) var isDateEqual = (currentDate - entryDate)===0; // if both is true we have a match if (isUserEqual && isDateEqual ){ entryexists = 1; } // debug logging if (debugging) { eb.print("currentUser = "+currentUser); eb.print("entryuser = "+entryuser); eb.print("isUserEqual = "+isUserEqual); //--------- eb.print("currentDate = "+currentDate); eb.print("entryDate = "+entryDate); eb.print("isDateEqual = "+isDateEqual); //--------- eb.print("entryexists = "+entryexists); } } if (entryexists>0){ // existing entry was found - so we try to edit it actionSB.append(""); actionSB.append(""); actionSB.append(duration); actionSB.append(""); actionSB.append("set by trigger"); if (entryexists>0){ // existing entry was found - closed the edit actionSB.append(""); } else{ // no existing entry was found - closed the create actionSB.append(""); } // close the actionlist actionSB.append(""); if (debugging) { eb.print("setting Time Entry"); } if (debugging) { eb.print(actionSB.toString()); } // make the api call using the default API user var s = eb.createAPISessionBean(); var cr = s.createAPICommandRunnerBean(); cr.setCommand("im", "settimeentries"); cr.addOption("actionDefinition",actionSB.toString()); var r = cr.execute(); } // keep Delta's history clean delta.clearFieldChange(durationField); if (debugging) { eb.exit("---------------end---------------"); }