Entitlement time not capturing on first update
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2023 08:56 AM
Hello All,
I need help with entitlement remaining time in units.
I aimed to calculate the time a user spends on the RITM table and deduct that time from the remaining time of entitlement. The after Business rule on update that I have written on sc_Req_item table for this is below:
(function executeRule(current, previous /*null when async*/ ) {
// var UniqueValue = current.getUniqueValue();
// gs.log('Unique value is :' + UniqueValue);
if (current.getTableName() == 'sc_req_item') {
var total_seconds = 0;
gs.log('Script is executing on table: ' + current.getTableName());
var time_entry_record = new GlideRecord('task_time_worked');
time_entry_record.addQuery('task', current.getUniqueValue());
time_entry_record.query();
while (time_entry_record.next()) {
total_seconds += time_entry_record.time_in_seconds;
}
gs.log('total_seconds: ' + total_seconds);
} else {
gs.log('Table is not RITM');
}
var total_time = total_seconds / 3600;
gs.log('in hours: ' + total_time);
var total_time_roundedup = Math.ceil(total_time);
gs.log('total_time_roundedup: ' + total_time_roundedup);
var encodedQuery = 'entitlement_name=Drawdown Hours^account=32739190c327dd1048917bf4e4013142';
var entitlementGR = new GlideRecord('service_entitlement');
entitlementGR.addEncodedQuery(encodedQuery);
entitlementGR.query();
if (entitlementGR.next()) {
var remainingTime = entitlementGR.remaining_units;
gs.log('remaining units are: ' + remainingTime);
remainingTime -= total_time_roundedup;
gs.log('current remaining unit' + remainingTime);
entitlementGR.setValue('remaining_units', remainingTime);
entitlementGR.update();
}
gs.log('exit the script');
})(current, previous);
So basically I am calculating the number of seconds a user works on the RITM table (stored in total_second log). The seconds are added as per every update when the state is closed. And the seconds are converted to hours and rounded off. Further the rounded value is deducted from the remaining units. The catch here is - If I make an update of 10 mins on RITM and close the state, the BR runs but the log total_second shows 0. Then again if I make an update on the closed RITM for 20 mins (total 20+10), the BR runs and log shows the previous 10 mins. So the time showed in log is one update previous which creates the problem in entitlement.
Similarly, if I make an update for 10 mins save it and again make an update for 20 mins and close the state then the time is showed in the logs as 30mins properly.
To conclude: Whatever update is made on RITM, the time captured for that update is one update less than the current update.
Note: I have changed the BR type to async as well to check but it didn't helped.
Need help in this ASAP. Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2023 11:09 AM
Hi, based on your requirements, I think an after BR running on 'task_time_worked' would be the most appropriate solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2023 11:28 AM
I tried after BR as well but it gives the same issue.
Async BR sometime gives proper time captured as below (but not always :():
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2023 12:02 PM
Hi, I think you have missed the intention of my last comment.
You are calculating a result based on new entries in the task_time_worked table.
Meaning your after BR needs to run on the task_time_worked table, so that it is triggered every time there is a task time worked record inserted (or updated if needed) otherwise you are triggering a script to lookup and calculate values which may not yet have been inserted into the DB.