Automatically set due date and priority based on customfield

Hi Eric,

I have created a custom field ‘severity’ where users can select a
severity level (for example severity levels 1-4) when creating a new
ticket. I would like to automatically assign predefined due dates and
priority levels based on the selection from the ‘severity’ field.

For example:

Severity 1 - Due 1 hour from now - Priority W
Severity 2 - Due 4 hours from now - Priority X
Severity 3 - Due 8 hours from now - Priority Y
Severity 4 - Due 24 hours from now - Priority Z

Thanks for the example code.

-Matt

[mailto:rt-users-bounces@lists.bestpractical.com] On Behalf Of Eric
HorneSent: Friday, December 16, 2005 1:52 PM
To: rt-users
Subject: Re: [rt-users] automatically set due date and priority based on
customfield

I’ve set the due date based on a due date embedded into the request. If
the user says “DateDue: Feb 28, 2006 2:00” it’ll set the duedate to that

specified date.

It’s not exactly what you are asking for, but maybe it helps get you
started. I think there is some date manipulation you can perform within
WebRT via the RT::Date class.

-eric

Condition: On Create
Template: Blank
Action:

my $AttachObj = $self->TransactionObj->Attachments->First;

go out if content is not text!

unless( $AttachObj->ContentType =~ /^text/ ) {
return 1;
}

my $content = $AttachObj->Content;

if ($content =~ m/^\QDate-Due:\E\s*([-0-9:\s]+[0-9])\s*$/m) {
$self->TicketObj->_Set(Field => “Due”, Value => $1);
}

Matt Nichols wrote:

Hello,

I’d like to automatically set the due date and priority of new tickets

based on a users selection of the custom field ‘severity’. The
creation
of the custom field ‘severity’ was the easy part. My understanding is
that the automatic setting of due date and priority based on this
custom
field can be accomplished with a scrip. Has anyone done anything
similar
to this?

Thanks.

-Matt


The rt-users Archives

Be sure to check out the RT Wiki at http://wiki.bestpractical.com

Download a free sample chapter of RT Essentials from O’Reilly Media at

http://rtbook.bestpractical.com

WE’RE COMING TO YOUR TOWN SOON - RT Training in Amsterdam, Boston and
San Francisco - Find out more at
http://bestpractical.com/services/training.html

http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-users

Be sure to check out the RT Wiki at http://wiki.bestpractical.com

Download a free sample chapter of RT Essentials from O’Reilly Media at
http://rtbook.bestpractical.com

WE’RE COMING TO YOUR TOWN SOON - RT Training in Amsterdam, Boston and
San Francisco - Find out more at
http://bestpractical.com/services/training.html

I haven’t actually tested this code, but I suspect you could do like his:

Condition: User Defined
Action: User Defined
Template: Blank

Condition:
($self->TransactionObj->Type eq “CustomField”) &&
($self->TransactionObj->Field eq 00);

replace 00 above with the CustomField NUMBER id.

Action:
my $date = new RT:Date();
my $val = $self->TicketObj->FirstCustomFieldValue(“CustomField”);
$date->SetToNow();

if ($val eq “1”) {
$date->AddSeconds(3600);
}
elsif ($val eq “2”) {

and so on

}

Why is there no SetDue function?

There may be a more efficient way to do this.

$self->TicketObj->_Set(Field => “Due”, Value => $date->AsString);
return 1;

Matt Nichols wrote:

Here’s the scrip we’ve got on our production server that sets both
priority and due dates on a ticket based on our ‘Urgency’ custom field.
It also uses the Business::Hours perl library to ensure that our
shorter-term tickets come due when we’re actually here.

Custom condition:
if (($self->TransactionObj->Type eq “CustomField”) and
($self->TransactionObj->Field eq “13”)) {
return(1);
} else {
return(undef);
}

Custom action cleanup:
my $ticket = $self->TicketObj;
my $CFName = ‘Urgency’;
my $QueueObj = $self->TicketObj->QueueObj;
my $CFObj = RT::CustomField->new ($QueueObj->CurrentUser);
my $duedate = RT::Date->new($RT::SystemUser);
my $bus_hours_duetime = time;

use Business::Hours;
my $hours = Business::Hours->new();
my $now = time;

$CFObj->LoadByNameAndQueue(Name=>$CFName, Queue=>$QueueObj->id);
unless ($CFObj->id) {
$RT::Logger->warning ("$CFName doesn’t exist in Queue " .
$QueueObj->Name);
return undef;
}

my $urgencyvalue = $self->TicketObj->FirstCustomFieldValue($CFObj->id);
if ($urgencyvalue eq ‘Emergency’) {
$self->TicketObj->SetPriority(‘100’);
$self->TicketObj->SetFinalPriority(‘100’);
$bus_hours_duetime = $hours->add_seconds ($now, 14400);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Urgent’) {
$self->TicketObj->SetPriority(‘90’);
$self->TicketObj->SetFinalPriority(‘99’);
$bus_hours_duetime = $hours->add_seconds ($now, 32400);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Time-Sensitive’) {
$self->TicketObj->SetPriority(‘89’);
$self->TicketObj->SetFinalPriority(‘89’);
$bus_hours_duetime = $hours->add_seconds ($now, 97200);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘High’) {
$self->TicketObj->SetPriority(‘75’);
$self->TicketObj->SetFinalPriority(‘88’);
$duedate->Set(Format=>‘unknown’, Value=>‘15 days’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Medium’) {
$self->TicketObj->SetPriority(‘50’);
$self->TicketObj->SetFinalPriority(‘74’);
$duedate->Set(Format=>‘unknown’, Value=>‘3 months’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Low’) {
$self->TicketObj->SetPriority(‘25’);
$self->TicketObj->SetFinalPriority(‘49’);
$duedate->Set(Format=>‘unknown’, Value=>‘6 months’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Very Low’) {
$self->TicketObj->SetPriority(‘1’);
$self->TicketObj->SetFinalPriority(‘24’);
$duedate->Set(Format=>‘unknown’, Value=>‘1 year’);
$self->TicketObj->SetDue($duedate->ISO);
} else {
}
return(1);

Mark Roedel
Web Programmer / Analyst
LeTourneau University-----Original Message-----
From: rt-users-bounces@lists.bestpractical.com
[mailto:rt-users-bounces@lists.bestpractical.com] On Behalf Of Matt
Nichols
Sent: Friday, December 16, 2005 1:37 PM
To: rt-users@lists.bestpractical.com
Subject: [rt-users] automatically set due date and priority based on
customfield

Hello,

I’d like to automatically set the due date and priority of new tickets
based on a users selection of the custom field ‘severity’. The creation
of
the custom field ‘severity’ was the easy part. My understanding is that
the automatic setting of due date and priority based on this custom
field
can be accomplished with a scrip. Has anyone done anything similar to
this?

Hey there,

i found this Scrip (originally posted by Mark Roedel) but can’t get it
to work. How do i create a corresponding “CustomField” for this?!

greets,
joey

Custom condition:
if (($self->TransactionObj->Type eq “CustomField”) and
($self->TransactionObj->Field eq “13”)) {
return(1);
} else {
return(undef);
}

Custom action cleanup:
my $ticket = $self->TicketObj;
my $CFName = ‘Urgency’;
my $QueueObj = $self->TicketObj->QueueObj;
my $CFObj = RT::CustomField->new ($QueueObj->CurrentUser);
my $duedate = RT::Date->new($RT::SystemUser);
my $bus_hours_duetime = time;

use Business::Hours;
my $hours = Business::Hours->new();
my $now = time;

$CFObj->LoadByNameAndQueue(Name=>$CFName, Queue=>$QueueObj->id);
unless ($CFObj->id) {
$RT::Logger->warning ("$CFName doesn’t exist in Queue " .
$QueueObj->Name);
return undef;
}

my $urgencyvalue = $self->TicketObj->FirstCustomFieldValue($CFObj->id);
if ($urgencyvalue eq ‘Emergency’) {
$self->TicketObj->SetPriority(‘100’);
$self->TicketObj->SetFinalPriority(‘100’);
$bus_hours_duetime = $hours->add_seconds ($now, 14400);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Urgent’) {
$self->TicketObj->SetPriority(‘90’);
$self->TicketObj->SetFinalPriority(‘99’);
$bus_hours_duetime = $hours->add_seconds ($now, 32400);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Time-Sensitive’) {
$self->TicketObj->SetPriority(‘89’);
$self->TicketObj->SetFinalPriority(‘89’);
$bus_hours_duetime = $hours->add_seconds ($now, 97200);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘High’) {
$self->TicketObj->SetPriority(‘75’);
$self->TicketObj->SetFinalPriority(‘88’);
$duedate->Set(Format=>‘unknown’, Value=>‘15 days’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Medium’) {
$self->TicketObj->SetPriority(‘50’);
$self->TicketObj->SetFinalPriority(‘74’);
$duedate->Set(Format=>‘unknown’, Value=>‘3 months’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Low’) {
$self->TicketObj->SetPriority(‘25’);
$self->TicketObj->SetFinalPriority(‘49’);
$duedate->Set(Format=>‘unknown’, Value=>‘6 months’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Very Low’) {
$self->TicketObj->SetPriority(‘1’);
$self->TicketObj->SetFinalPriority(‘24’);
$duedate->Set(Format=>‘unknown’, Value=>‘1 year’);
$self->TicketObj->SetDue($duedate->ISO);
} else {
}
return(1);

In our case, it’s just an “Select one value”-type custom field applied
to tickets in a particular queue.

Looking at the code, the first thing I can see that might trip you up is
in the custom condition, which refers to the custom field by its
internal ID (“13”) rather than by its name. If the problem you’re
having is just that the scrip doesn’t fire, that’s probably the first
place I’d look. (You can find the internal ID of your custom field by
looking at the id parameter in the URL while you’re editing it in the
configuration screens.)

If that doesn’t help, perhaps a bit more detail as to how the scrip is
failing for you would be helpful…

Mark Roedel
Senior Programmer / Analyst
LeTourneau University-----Original Message-----
From: joey [mailto:j0ey@j0ey.de]
Sent: Tuesday, November 14, 2006 8:57 AM
To: RT-Users@lists.bestpractical.com
Cc: Roedel, Mark
Subject: automatically set due date and priority based on customfield

Hey there,

i found this Scrip (originally posted by Mark Roedel) but can’t get it
to work. How do i create a corresponding “CustomField” for this?!

greets,
joey

Custom condition:
if (($self->TransactionObj->Type eq “CustomField”) and
($self->TransactionObj->Field eq “13”)) { return(1); } else {
return(undef); }

Custom action cleanup:
my $ticket = $self->TicketObj;
my $CFName = ‘Urgency’;
my $QueueObj = $self->TicketObj->QueueObj; my $CFObj =
RT::CustomField->new ($QueueObj->CurrentUser); my $duedate =
RT::Date->new($RT::SystemUser); my $bus_hours_duetime = time;

use Business::Hours;
my $hours = Business::Hours->new();
my $now = time;

$CFObj->LoadByNameAndQueue(Name=>$CFName, Queue=>$QueueObj->id); unless
($CFObj->id) { $RT::Logger->warning ("$CFName doesn’t exist in Queue " .
$QueueObj->Name);
return undef;
}

my $urgencyvalue = $self->TicketObj->FirstCustomFieldValue($CFObj->id);
if ($urgencyvalue eq ‘Emergency’) {
$self->TicketObj->SetPriority(‘100’);
$self->TicketObj->SetFinalPriority(‘100’);
$bus_hours_duetime = $hours->add_seconds ($now, 14400);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Urgent’) {
$self->TicketObj->SetPriority(‘90’);
$self->TicketObj->SetFinalPriority(‘99’);
$bus_hours_duetime = $hours->add_seconds ($now, 32400);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Time-Sensitive’) {
$self->TicketObj->SetPriority(‘89’);
$self->TicketObj->SetFinalPriority(‘89’);
$bus_hours_duetime = $hours->add_seconds ($now, 97200);
$duedate->Set(Format=>‘unix’, Value=>$bus_hours_duetime);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘High’) {
$self->TicketObj->SetPriority(‘75’);
$self->TicketObj->SetFinalPriority(‘88’);
$duedate->Set(Format=>‘unknown’, Value=>‘15 days’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Medium’) {
$self->TicketObj->SetPriority(‘50’);
$self->TicketObj->SetFinalPriority(‘74’);
$duedate->Set(Format=>‘unknown’, Value=>‘3 months’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Low’) {
$self->TicketObj->SetPriority(‘25’);
$self->TicketObj->SetFinalPriority(‘49’);
$duedate->Set(Format=>‘unknown’, Value=>‘6 months’);
$self->TicketObj->SetDue($duedate->ISO);
} elsif ($urgencyvalue eq ‘Very Low’) {
$self->TicketObj->SetPriority(‘1’);
$self->TicketObj->SetFinalPriority(‘24’);
$duedate->Set(Format=>‘unknown’, Value=>‘1 year’);
$self->TicketObj->SetDue($duedate->ISO);
} else {
}
return(1);

Hey,

thanks for the fast reply!

I changed the internal id to “1” (that’s what i saw in the URL while
editing the CustomField.). sadly it’s not working :confused:

the rt.log says:
Nov 14 16:50:17 rt2 RT: About to think about scrips for transaction #20772
Nov 14 16:50:17 rt2 RT: About to think about scrips for transaction #20773
Nov 14 16:50:18 rt2 RT: About to think about scrips for transaction #20774
Nov 14 16:50:18 rt2 RT: About to prepare scrips for transaction #20774
Nov 14 16:50:18 rt2 RT: Found 3 scrips
Nov 14 16:50:18 rt2 RT: About to commit scrips for transaction #20774
Nov 14 16:50:18 rt2 RT:
rt-3.6.1-20714-1163519418-685.3937-114-0@scan-plus.de #3937/20774 -
Scrip 114 Create - ALL (/usr/local/rt3//lib/RT/Action/SendEmail.pm:238)
Nov 14 16:50:19 rt2 RT: About to think about scrips for transaction #20775
Nov 14 16:50:19 rt2 RT:
rt-3.6.1-20714-1163519418-685.3937-114-0@xyz.de sent Bcc:
test@test.com (/usr/local/rt3//lib/RT/Action/SendEmail.pm:325)
Nov 14 16:50:19 rt2 RT: Ticket 3937 created in queue ‘testqueue’ by root
(/usr/local/rt3//lib/RT/Ticket_Overlay.pm:755)

any other ideas? :confused:

Roedel, Mark wrote:

I’m not positive from looking at your log there, but it looks to me like
the result of creating a new ticket…is that so? (The custom condition
won’t fire on ticket creation, just on a change to the custom field on
an existing ticket – I’ve also got a separate scrip defined with the
built-in “on create” condition and the same custom action code.)

Does that help?

Mark Roedel
Senior Programmer / Analyst
LeTourneau University
Longview, Texas USAFrom: joey [mailto:j0ey@j0ey.de]
Sent: Tuesday, November 14, 2006 9:55 AM
To: Roedel, Mark
Cc: RT-Users@lists.bestpractical.com
Subject: Re: automatically set due date and priority based on
customfield

Hey,

thanks for the fast reply!

I changed the internal id to “1” (that’s what i saw in the URL while
editing the CustomField.). sadly it’s not working :confused:

the rt.log says:
Nov 14 16:50:17 rt2 RT: About to think about scrips for transaction
#20772 Nov 14 16:50:17 rt2 RT: About to think about scrips for
transaction #20773 Nov 14 16:50:18 rt2 RT: About to think about scrips
for transaction #20774 Nov 14 16:50:18 rt2 RT: About to prepare scrips
for transaction #20774 Nov 14 16:50:18 rt2 RT: Found 3 scrips Nov 14
16:50:18 rt2 RT: About to commit scrips for transaction #20774 Nov 14
16:50:18 rt2 RT:
rt-3.6.1-20714-1163519418-685.3937-114-0@scan-plus.de #3937/20774 -
Scrip 114 Create - ALL (/usr/local/rt3//lib/RT/Action/SendEmail.pm:238)
Nov 14 16:50:19 rt2 RT: About to think about scrips for transaction
#20775 Nov 14 16:50:19 rt2 RT:
rt-3.6.1-20714-1163519418-685.3937-114-0@xyz.de sent Bcc:
test@test.com (/usr/local/rt3//lib/RT/Action/SendEmail.pm:325)
Nov 14 16:50:19 rt2 RT: Ticket 3937 created in queue ‘testqueue’ by root
(/usr/local/rt3//lib/RT/Ticket_Overlay.pm:755)

any other ideas? :confused:

Roedel, Mark wrote:

Looking at the code, the first thing I can see that might trip you up
is in the custom condition, which refers to the custom field by its
internal ID (“13”) rather than by its name. If the problem you’re
having is just that the scrip doesn’t fire, that’s probably the first
place I’d look. (You can find the internal ID of your custom field by

looking at the id parameter in the URL while you’re editing it in the
configuration screens.)

If that doesn’t help, perhaps a bit more detail as to how the scrip is

failing for you would be helpful…

Yes,

that helped a lot :slight_smile: but its still not working properly.

  1. i created a ticket with no value in the CustomField
  2. i modify the value to “Low”

Result:

  • User asked for an unknown update type for custom field Urgency for
    RT::Ticket object #3939
  • Urgency Low added
  1. change “Low” to “High”

Result:
* User asked for an unknown update type for custom field Urgency
for RT::Ticket object #3939
* Urgency Low changed to Emergency
* Custom field value Low could not be found for custom field Urgency

Do the values need to be in one “Category”? (-> Editing CustomField
Urgency).

Roedel, Mark wrote: