Calculating time between custom fields

Hello there :slight_smile:

At the moment I am writing a script for RT, which tries to

  1. get the value of a field (DateTime)

  2. adds the hours

  3. outputs the values to a other custom Field

     my $getSLAField  = (2,3,4);
     my $setDueDateField = (5,6,7);
     my $index = 0;
    foreach my $id (@getSLAField) 
    {
    RT::Logger->info("Starting SLA ID :  " + $id);
    my $getID = RT::CustomFieldValue->$id;
    my $getSLAFieldValue = $getID->Name;
    
    my $createDeadlineValue = $setDueDateField[$index];
    my $getCurrentTime = RT::Date->new( RT->DateTimeObj );
    RT::Logger->info("X Value of current Time : " + $getCurrentTime);
    
    my $DueDate= $getCurrentTime -> add (hours => $getSLAFieldValue);
    RT::Logger->info("Y Value = " + $DueDate);
    
    my $setDueDateValue = RT::CustomFieldValue->new( RT->SetCustomFieldObj->SetName->$DueDate);
    $index++;
    }
    

Now I also checked out the SLA built-in option, however I would need it to be different for every queue

Unfortunately I don’t have access to any of the logs,
Any input is greatly appreciated :slight_smile:

Is that the whole script? There is no variable for @getSLAField as Perl interprets $getSLAField as a different scalar variable.

I’d suggest running a local test Perl script with placeholder content just to ensure the Perl syntax is correct:

use strict;
use warnings;

my @getSLAField  = (2,3,4);

foreach my $id (@getSLAField) {
  print "$id\n";
}
1 Like

Ah yes, sorry my bad, I didn’t update the side of my rt-script from the one i had for local perl :sweat_smile:

Ultimately I would like to check if these commands would work in an rt script

my $getID = RT::CustomFieldValue->$id;
my $getSLAFieldValue = $getID->Name; 

my $getCurrentTime = RT::Date->new( RT->DateTimeObj );
my $DeadLine = $getCurrentTime -> add (hours => $getSLAFieldValue);
my $convertData2String = "$DeadLine";
my $setDueDateValue = RT::CustomFieldValue->new( RT->SetCustomFieldObj->SetName->$convertData2String );

I am 99% a lot of that code won’t work:

my $getID = RT::CustomFieldValue->$id;

Something like this isn’t calling the method “id” on the CustomFieldValue object, but instead expecting a variable called $id.

Then this code is pseudocode:

my $getCurrentTime = RT::Date->new( RT->DateTimeObj );
my $DeadLine = $getCurrentTime -> add (hours => $getSLAFieldValue);
my $convertData2String = "$DeadLine";
my $setDueDateValue = RT::CustomFieldValue->new( RT->SetCustomFieldObj->SetName->$convertData2String );

Documentation for the “Objects” in RT can be found here:

https://docs.bestpractical.com/rt/5.0.0/index.html

I can’t re-write the whole thing but for setting something like due based on some other field ( Created date for this example ) would look like this:

# This gives you a RT::Date object
# https://docs.bestpractical.com/rt/5.0.0/RT/Date.html
my $created_date = $self->TicketObj->CreatedObj;

# Maybe add 7 days to the created date and set due to that?
$created_date->AddDays( 7 );

my ($ret, $msg) = $self->TicketObj->SetDue( $created_date->ISO );
RT::Logger->error( "Could not set date value: $msg" ) unless $ret;

You can also set date custom field values, I just used Due in that example.

1 Like

It actually is a great help, thank you so much! :slight_smile:

Once I get the script working, i’ll post it here just in case anyone might want to do the same in the future :slight_smile:

1 Like

Hey there :slight_smile:

So I managed to figure it out and there is an explanation + code below of everything :slight_smile:
Next step is to now try convert the DateTime ISO for business days and hours :slight_smile:

RT v. 4.4.0

desired outcome:
To contain custom fields (SLA) for different queues & calculate a DateTime a custom field (Due Date) per queue

prep:

  • Create custom field , example

Name: SLA (hours) or SLA (days)

  1. Type: “select one value”
  2. Val: Digits
  3. Default Values (you can enter the values one by one from the bottom of the page)
  4. Keep note of the Field ID ( inside the ‘All Custom Field’ Page)
  • Create another custom field , example

Name: SLA Due Date

  1. Type: “select one value”
  2. Val: Digits
  3. Default Values (you can enter the values one by one from the bottom of the page)
  4. Keep note of the Field ID ( inside the ‘All Custom Field’ Page)
  • Attach the Custom Field to Queues
  1. Enter the Queue Config
  2. Custom Fields -> Tickets
  3. Select the Custom Tickets (SLA (Hours) & SLA Due Date)
  4. Click Submit

Create Script

  1. In the Top Menu Bar, Admin -> Scripts -> Create
  2. Enter Desc.
  3. Cond: On Create
  4. Action: User Defined
  5. Template: Black

Custom Condition: Leave Blank
Custom Action Prep code: return 1;

Custom action commit code:

# This gives you a RT::Date object (Thanks knation)
my $created_date = $self->TicketObj->CreatedObj;
# CustomField SLA Due Date
my $set_custom_field_value = RT::CustomField->new($RT::SystemUser);
#  Creates ticket object (the one you're going to be using)
my $ticket = $self->TicketObj;
# Get Value of Custom Field using its ID - SLA (Hours) or SLA (Days)
my $SLA = $self->TicketObj->FirstCustomFieldValue(2);
# Load up the Custom Field using its ID - SLA-Date
$set_custom_field_value->Load(5);

# If you want to use days instead of hours ignone theses 2 lines
$SLA = $SLA+1;
$SLA = $SLA / 24;

# Adds hours / days to the RT::Date Object
$created_date->AddDays( $SLA );

# Assigns the newly edited RT::Date Object in ISO format into the SLA Due Date Custom Field 
my ($ret, $msg) = $ticket->AddCustomFieldValue( Field=>$set_custom_field_value, Value=>$created_date->ISO,RecordTransaction=>0 );
# Logs if there is an error
RT::Logger->error( "Could not set date value: $msg" ) unless $ret;
# exit script
return 1;
  • Attach Script to Queue
  1. In the Top Menu Bar, Admin -> Scripts -> Create
  2. Choose Queue
  3. Scripts -> Select
  4. Find newly created Script in the section ‘Not applied scripts’
  5. Tick it
  6. Update button

When you create a new ticket you’ll see 2 new custom fields

  • SLA (Hours) / SLA (Days)
  • SLA Due Date

Now when you create a ticket enter the details, choose the values of SLA and click create
Then you’ll use the calculated SLA Due Date.

You can then repeat this process to have different values for different queues :slight_smile:

Credit to knation & GreenJimll for their time to explain to me the how RT scripting works :smiley: