Business rule not always running as expected

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 12:36 PM
We have a before business rule that should always run on insert/update (no other conditions) into the task_time_worked table via a time worked update on tasks, but about 1% of the time it does not run. The rule is intended to create/update time cards, but because of this issue, it's not doing so consistently. The record does get inserted, just triggers no business rules.
There does not appear to be any kind of a pattern to when this happens - it seems completely random.
I have searched high and low for other rules that may be preventing this rule from running (containing setWorkflow(false))
I'm at my wits end and need some advice on other ways to troubleshoot. I can't reproduce what's happening... and HI won't help; the rule is custom because we needed some additional functionality that the OOB rule did not provide.
Any ideas?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 01:08 PM
Can you share the BR Code
Pranav

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 01:15 PM
I can, but as I stated above, the rule is not firing at all, nor is another rule that I've written to test 'before insert' with order 1. So the issue isn't the rule itself, which is working 99+% of the time, it's that for 1%~ of the inserts, no rules on that table are being triggered.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 01:52 PM
I'm going to try to clarify the issue:
1. A user enters time in the time_worked field on any task.
2. This creates a task_time_worked record and works 100% of the time.
3. The creation of the task_time_worked record *should* trigger a 'before' business rule, with order of 1 - the rule simply creates a log entry - this only happens ~99% of the time.
I have removed my original rule from the scenario, because its contents have nothing to do with this issue, the issue being that ~1% of the time, the insertion of a task_time_worked record triggers ZERO business rules.
As I stated above, I have searched all rules running on the task_time_worked table, and none of them have a setWorkflow(false)statement. I have also search ALL business rules for the phrases 'task_time_worked' and 'setWorkflow(false)' to rule out rules on other tables that may be modifying task_time_worked records and then stopping any further business rules from running.
I don't know what else to look for....

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 01:59 PM
Hi
When you can post screenshot of the BR, and several people here ask for it, why don't you raise your chances for a resulting answer by just submitting the screenshots here?
BR
Dirk

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2020 02:21 PM
Because the rule is not running at all, therefore its contents should have no bearing. But here it is, anyway....
(function executeRule(current, previous /*null when async*/ ) {
gs.log('TCLOG: Calling function for ' + current.user.name + ' for task ' + current.task.number + ' for ' + current.time_in_seconds, "KC");
timeCardUpdateV2();
function timeCardUpdateV2() {
//gs.log('TCLOG: Running time card update for ' + current.user.name + ' for task ' + current.task.number + 'for ' + current.time_in_seconds, "KC");
//admins should never add time to a time worked record for a user - adding time will not update their time card. The user can add time themselves, using standard methods
var comment = current.comments;
var timeCalc;
if (current.operation() == 'insert') {
timeCalc = 'add';
} else if (current.operation() == 'update' && current.time_in_seconds < previous.time_in_seconds) {
timeCalc = 'subtract';
} else
return;
var timeDiff = 0;
if (timeCalc == 'add') {
timeDiff = current.time_in_seconds;
timeDiff = timeDiff / 60 / 60;
timeDiff = parseFloat(timeDiff).toFixed(2);
} else {
timeDiff = previous.time_in_seconds - current.time_in_seconds;
timeDiff = timeDiff / 60 / 60;
timeDiff = parseFloat(timeDiff).toFixed(2);
}
//determine task type and get company and contract values
var opTask = false;
var serviceContract;
var company;
if (!current.u_operational_task.nil())
opTask = true;
if (opTask) {
var oTask = current.u_operational_task.getRefRecord();
serviceContract = oTask.u_service_contract;
company = oTask.u_company;
} else {
var task = current.task.getRefRecord();
company = task.company;
serviceContract = task.u_service_contract;
}
//get the date and time entered
var created;
if (current.u_date_performed.nil()) {
//this will only work correctly if the user updates the time card themselves. If an admin updates it for them, the time zone may be different and update the wrong time card
created = new GlideDateTime(current.sys_created_on);
var tzOffset = new GlideDateTime(created.getTZOffset() * -1);
created.add(tzOffset); //converts date from system time to user's local time
} else {
created = new GlideDateTime(current.u_date_performed + " 10:00:00"); //returns YYYY-MM-DD HH:MM:SS
}
var year = created.getYearLocalTime();
var yearString = year.toString();
var month = created.getMonthLocalTime();
var monthString = month.toString();
if (month < 10) {
var monthArray = ["", "01", "02", "03", "04", "05", "06", "07", "08", "09"];
monthString = monthArray[month];
}
var day = created.getDayOfWeek();
var dayNames = ["", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
//set the necessary variables for creating or updating the time card
var user = current.user;
var dayOfWeek = dayNames[day];
var weekStart = new DateTimeUtils().getWeekStart(created, 1);
var reportingPeriod = yearString + monthString;
//check all time cards to see if user is trying to add more than 24 hours on the current day
var cardID;
var totalForDay = 0;
if (timeCalc == 'add')
totalForDay = timeDiff;
else
totalForDay = timeDiff * -1;
var tCard = new GlideRecord('time_card');
tCard.addQuery('user', user);
tCard.addQuery('week_starts_on', weekStart);
tCard.query();
while (tCard.next()) {
var timeCardObj = new Object(tCard);
totalForDay = parseFloat(totalForDay) + parseFloat(timeCardObj[dayOfWeek]);
}
if (totalForDay > 24) {
cardID = 'invalid_update';
} else
cardID = findTimeCard();
if (cardID == 'invalid_update') {
var dayNamesCap = ["", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var dayOfWeekCap = dayNamesCap[day];
var timeCardLink = 'time_card_list.do?sysparm_query=user=' + user + '%5Eweek_starts_on=' + weekStart;
gs.addErrorMessage("You are attempting to add more than 24 hours to your " + dayOfWeekCap + " time card(s) for this or a past week. <b>Your note for this entry was added, but the time was not.</b> Please review your time cards <a href = " + "'" + timeCardLink + "'>here</a>, then re-enter the correct amount of time on this task, taking care not to duplicate notes if possible.");
gs.log('TCLOG: Time entry error for ' + current.user.name + ' on task ' + current.task.number + ' for ' + current.time_in_seconds,"KC");
current.setAbortAction(true);
//var deleteTimeWorked = new timeCardUtil().deleteTimeWorked(current.sys_id); //deletes this time worked record
} else if (cardID == 'no_time_card') {
//gs.log('TCLOG: Creating time card for ' + current.user.name + ' on task ' + current.task.number + ' for ' + current.time_in_seconds + ' seconds', "KC");
current.u_time_card = createTimeCard();
} else {
//gs.log('TCLOG: Updating time card for ' + current.user.name + ' on task ' + current.task.number + ' for ' + current.time_in_seconds + ' seconds', "KC");
current.u_time_card = updateTimeCard(cardID);
}
//clear the Work performed on field in the task
if (!current.u_date_performed.nil()) {
if (opTask) {
var opTsk = new GlideRecord('personal_task');
opTsk.get('sys_id', current.u_operational_task);
opTsk.u_work_performed_on = '';
opTsk.update();
} else {
var target = new GlideRecord('task');
target.get('sys_id', current.task);
target.u_work_performed_on = '';
target.update();
}
}
if (current.operation() == 'update' && current.time_in_seconds == 0)
current.deleteRecord();
function findTimeCard() {
var timeTotal;
var existingTime;
var tc = new GlideRecord('time_card');
tc.addQuery('user', user);
tc.addQuery('u_reporting_period', reportingPeriod);
tc.addQuery('week_starts_on', weekStart);
if (opTask)
tc.addQuery('u_operational_task', current.u_operational_task);
else
tc.addQuery('task', current.task);
tc.query();
if (tc.next()) {
return tc.sys_id;
} else {
return 'no_time_card';
}
}
function updateTimeCard() {
var timeCard = new GlideRecord('time_card');
timeCard.get(cardID);
var tcTotal = timeCard.total;
if (dayOfWeek == 'monday') {
if (timeCalc == 'add')
timeCard.monday = parseFloat(timeCard.monday) + parseFloat(timeDiff);
else
timeCard.monday = parseFloat(timeCard.monday) - parseFloat(timeDiff);
} else if (dayOfWeek == 'tuesday') {
if (timeCalc == 'add')
timeCard.tuesday = parseFloat(timeCard.tuesday) + parseFloat(timeDiff);
else
timeCard.tuesday = parseFloat(timeCard.tuesday) - parseFloat(timeDiff);
} else if (dayOfWeek == 'wednesday') {
if (timeCalc == 'add')
timeCard.wednesday = parseFloat(timeCard.wednesday) + parseFloat(timeDiff);
else
timeCard.wednesday = parseFloat(timeCard.wednesday) - parseFloat(timeDiff);
} else if (dayOfWeek == 'thursday') {
if (timeCalc == 'add')
timeCard.thursday = parseFloat(timeCard.thursday) + parseFloat(timeDiff);
else
timeCard.thursday = parseFloat(timeCard.thursday) - parseFloat(timeDiff);
} else if (dayOfWeek == 'friday') {
if (timeCalc == 'add')
timeCard.friday = parseFloat(timeCard.friday) + parseFloat(timeDiff);
else
timeCard.friday = parseFloat(timeCard.friday) - parseFloat(timeDiff);
} else if (dayOfWeek == 'saturday') {
if (timeCalc == 'add')
timeCard.saturday = parseFloat(timeCard.saturday) + parseFloat(timeDiff);
else
timeCard.saturday = parseFloat(timeCard.saturday) - parseFloat(timeDiff);
} else if (dayOfWeek == 'sunday') {
if (timeCalc == 'add')
timeCard.sunday = parseFloat(timeCard.sunday) + parseFloat(timeDiff);
else
timeCard.sunday = parseFloat(timeCard.sunday) - parseFloat(timeDiff);
}
if (timeCalc == 'subtract') {
tcTotal = parseFloat(tcTotal) - parseFloat(timeDiff);
}
if (tcTotal <= 0 && timeCalc == 'subtract') {
timeCard.deleteRecord();
gs.addInfoMessage('All time removed from time card, card deleted.');
} else {
timeCard.u_task_notes.setJournalEntry(comment);
timeCard.update();
}
return timeCard.sys_id;
}
function createTimeCard() {
var createTC = new GlideRecord('time_card');
createTC.initialize();
if (opTask)
createTC.u_operational_task = current.u_operational_task;
else
createTC.task = current.task;
createTC.user = user;
createTC.u_company = company;
createTC.u_service_contract = serviceContract;
createTC.u_reporting_period = reportingPeriod;
createTC.week_starts_on = weekStart;
if (dayOfWeek == 'monday') {
createTC.monday = timeDiff;
} else if (dayOfWeek == 'tuesday') {
createTC.tuesday = timeDiff;
} else if (dayOfWeek == 'wednesday') {
createTC.wednesday = timeDiff;
} else if (dayOfWeek == 'thursday') {
createTC.thursday = timeDiff;
} else if (dayOfWeek == 'friday') {
createTC.friday = timeDiff;
} else if (dayOfWeek == 'saturday') {
createTC.saturday = timeDiff;
} else if (dayOfWeek == 'sunday') {
createTC.sunday = timeDiff;
} else
gs.log('TCLOG: Day not found', "KC");
createTC.total = timeDiff;
createTC.category = 'task_work';
createTC.u_task_notes = comment;
var timeCardID = createTC.insert();
return timeCardID;
}
}
})(current, previous);