How to calculate business elapsed time ?

prash4
Tera Expert

Hi Community! 

I am struggling to calculate business elapsed time in Taksa. 

Requirement: some records are not updated with stop time, and we are trying to calculate business elapsed duration with start time and adding that time to stop time.

can someone please help me with the script. this is really important to me.

 

1 ACCEPTED SOLUTION

@prash4 

 

please run this fix script - 

(function() {
  var taskGR = new GlideRecord('task');
  taskGR.addNullQuery('stop');
  taskGR.addQuery('task_sla.state', 'false');
  taskGR.addQuery('stage', 'completed');
  taskGR.query();

  while (taskGR.next()) {
    var start = new GlideDateTime(taskGR.getValue('start'));
    var elapsedTime = new GlideDuration(taskGR.getValue('business_stc'));

    if (!start.nil() && !elapsedTime.nil()) {
      var stop = new GlideDateTime(start);
      stop.add(elapsedTime);
      taskGR.setValue('stop', stop);
      taskGR.update();
    }
  }
})();

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

View solution in original post

7 REPLIES 7

@prash4 

 

please run this fix script - 

(function() {
  var taskGR = new GlideRecord('task');
  taskGR.addNullQuery('stop');
  taskGR.addQuery('task_sla.state', 'false');
  taskGR.addQuery('stage', 'completed');
  taskGR.query();

  while (taskGR.next()) {
    var start = new GlideDateTime(taskGR.getValue('start'));
    var elapsedTime = new GlideDuration(taskGR.getValue('business_stc'));

    if (!start.nil() && !elapsedTime.nil()) {
      var stop = new GlideDateTime(start);
      stop.add(elapsedTime);
      taskGR.setValue('stop', stop);
      taskGR.update();
    }
  }
})();

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

wojasso
Giga Guru

Let me know if below helps:

 

1. Navigate to "Business Rules" in ServiceNow:

System Definition > Business Rules
2. Click on "New" to create a new Business Rule.

3. Provide a Name and Description for the Business Rule.

4. Set the "Applies to" table to the table where your records with start and stop time properties are stored.

5. In the "When to run" section, choose "Before" and the appropriate "Insert" and "Update" options based on your requirements.

6. In the "Advanced" tab, enter the following script:

(function executeRule(current, previous /*optional*/) {
// Check if stop time is not provided (empty or null)
if (!current.stop) {
var start = new GlideDateTime(current.start);
var end = new GlideDateTime();
// Calculate the elapsed time in milliseconds
var elapsedMilliseconds = end.getNumericValue() - start.getNumericValue();
// Add the elapsed time to the start time to set the stop time
var stop = new GlideDateTime();
stop.setNumericValue(start.getNumericValue() + elapsedMilliseconds);
// Update the current record with the calculated stop time
current.stop = stop;
}
})(current, previous);

7. Save the Business Rule.

This Business Rule will run before the record is inserted or updated, and it will calculate the elapsed time between the start time and the current time if the stop time is not provided. It will then set the calculated stop time in the record.

Please note that you might need to adjust the script based on the actual field names and data types used in your table. Also, consider any additional conditions or validations you might need in your specific scenario.

 

Alizaa
Tera Contributor

Of course, I'd be happy to help you with a script to calculate business elapsed time in Taksa. To clarify, you want to calculate the elapsed time for records where the stop time is missing, right? And you want to add the elapsed time to the missing stop time using the provided start time?

Here's a Python script that demonstrates how you could achieve this: 

from datetime import datetime, timedelta

# Sample data: Replace this with your actual data
records = [
{"start_time": "2023-08-09 09:00:00", "stop_time": "2023-08-09 10:30:00"},
{"start_time": "2023-08-09 11:00:00", "stop_time": None},
# Add more records as needed
]

# Business work hours (adjust as needed)
work_start_time = datetime.strptime("09:00:00", "%H:%M:%S").time()
work_end_time = datetime.strptime("18:00:00", "%H:%M:%S").time()

# Calculate business elapsed time for records
for record in records:
start_time = datetime.strptime(record["start_time"], "%Y-%m-%d %H:%M:%S")

if record["stop_time"]:
stop_time = datetime.strptime(record["stop_time"], "%Y-%m-%d %H:%M:%S")
else:
current_time = datetime.now()
if current_time.time() > work_end_time:
stop_time = datetime.combine(current_time.date(), work_end_time)
else:
stop_time = current_time

elapsed_time = stop_time - start_time

# Adjust elapsed time for non-business hours
if start_time.time() < work_start_time:
elapsed_time -= timedelta(hours=work_start_time.hour, minutes=work_start_time.minute)
if stop_time.time() > work_end_time:
elapsed_time -= timedelta(hours=24) - timedelta(hours=work_end_time.hour, minutes=work_end_time.minute)

# Print or save the elapsed time
print(f"Start Time: {start_time}, Stop Time: {stop_time}, Elapsed Time: {elapsed_time}")

# You can modify this script according to your specific requirements and integrate it into your Taksa application.

This script assumes that you have a list of records, each containing a "start_time" and "stop_time" (which can be None for missing stop times). It calculates the elapsed time while accounting for business work hours (specified by work_start_time and work_end_time), and handles cases where the start or stop time falls outside of business hours.

Please replace the sample data with your actual records and adjust the work hours as needed. Let me know if you need further assistance or modifications!

 

The poe chrome calc is a valuable tool for players of the popular action RPG. It assists in crafting and optimizing gear by calculating the number of chromatic orbs needed to achieve desired socket colors. A time-saving asset in the game's intricate item customization process.