Hello there
At the moment I am writing a script for RT, which tries to
-
get the value of a field (DateTime)
-
adds the hours
-
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
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
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!
Once I get the script working, iâll post it here just in case anyone might want to do the same in the future
1 Like
Hey there
So I managed to figure it out and there is an explanation + code below of everything
Next step is to now try convert the DateTime ISO for business days and hours
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)
- Type: âselect one valueâ
- Val: Digits
- Default Values (you can enter the values one by one from the bottom of the page)
- Keep note of the Field ID ( inside the âAll Custom Fieldâ Page)
- Create another custom field , example
Name: SLA Due Date
- Type: âselect one valueâ
- Val: Digits
- Default Values (you can enter the values one by one from the bottom of the page)
- Keep note of the Field ID ( inside the âAll Custom Fieldâ Page)
- Attach the Custom Field to Queues
- Enter the Queue Config
- Custom Fields -> Tickets
- Select the Custom Tickets (SLA (Hours) & SLA Due Date)
- Click Submit
Create Script
- In the Top Menu Bar, Admin -> Scripts -> Create
- Enter Desc.
- Cond: On Create
- Action: User Defined
- 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;
- In the Top Menu Bar, Admin -> Scripts -> Create
- Choose Queue
- Scripts -> Select
- Find newly created Script in the section âNot applied scriptsâ
- Tick it
- 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
Credit to knation & GreenJimll for their time to explain to me the how RT scripting works