Scrip to add CC is not sending email

I’ve created a scrip based on http://requesttracker.wikia.com/wiki/OnCreateAddGroupCc, which automatically adds CCs based on a custom field.

Adding the CC works fine, and they get copied on further correspondence. But they don’t get the initial email like they would if I put them in the actual CC box when creating the ticket.
I assume what happens is all the scrips run for the initial ticket, including the my custom one and the one that sends emails to CCs. But even though mine runs first, it still doesn’t see the added CC, since that’s a separate transaction.

I’ve searched for a scrip that would send the original ticket to new CC’s when they are added, but haven’t come up with anything. How can I make sure the new CCs get an email when they are added by my scrip instead of the CC box during ticket creation?

RT version 4.2.0. All other scrips are the default ones, nothing has been changed except adding mine.

In case I’m doing something wrong with my scrip, the code is below.

Thanks,

Grant Emsley

Description: 01 On Create Add Department CCs
Condition: User Defined
Action: User Defined
Template: Blank

Custom Condition:

$RT::Logger->info(“On Create Add Department CCs: entering condition check”);
if(
(($self->TransactionObj->Type eq “Create”) || ($self->TransactionObj->Type eq “CustomField”))
&&($self->TicketObj->FirstCustomFieldValue(‘Department’))
&&($self->TicketObj->FirstCustomFieldValue(‘Department’) ne ‘None’)
) {
$RT::Logger->info("On Create Add Department CCs: met conditions - Department is " . $self->TicketObj->FirstCustomFieldValue(‘Department’));
return 1;
} else {
return undef;
}

Custom action preparation code:

my $groupname = ‘CC-’ . $self->TicketObj->FirstCustomFieldValue(‘Department’);

Load the custom group from RT

my $groupObj = RT::Group->new($RT::SystemUser);
$groupObj->LoadUserDefinedGroup($groupname);
return undef unless $groupObj;

Instead of adding the group, find and add the members of the group

This way, we can exclude the requestor themselves from being CC’ed.

my $groupMembersObj = $groupObj->UserMembersObj;

$RT::Logger->info(“Finding members of group $groupname for ticket #” . $self->TicketObj->id);
my $userObj;
while ($userObj = $groupMembersObj->Next) {
if(($self->TicketObj->IsRequestor($userObj->PrincipalId)) or ($self->TicketObj->IsCc($userObj->PrincipalId))) {
$RT::Logger->info("On Create Add Department CCs: Not adding " . $userObj->Name . ", already on ticket " . $self->TicketObj->id);
} else {
$RT::Logger->info("On Create Add Department CCs: Adding " . $userObj->Name . " to ticket " . $self->TicketObj->id);
my ($success, $msg) = $self->TicketObj->AddWatcher(
Type => “Cc”,
PrincipalId => $userObj->PrincipalId);
if(!$success) {
$RT::Logger->info("On Create Add Department CCs: Could not add " . $userObj->Name . " to " . $self->TicketObj->id . "Got: " . $msg);
}
}
}
return 1;

For anyone who comes across this in the future:

Change the notification scripts to run in the Transaction Batch stage instead of Transaction Create. All the transaction create scrips are prepared, then they all get committed. The notification scripts decide who to send the emails to in the preparation step, before the new CC has actually been committed.

The Transaction Batch stage starts after all the transaction create scrips have been committed, so it will see the new watcher.-----Original Message-----
From: Grant Emsley
Sent: Monday, November 04, 2013 11:02 AM
To: rt-users@lists.bestpractical.com
Subject: Scrip to add CC is not sending email

I’ve created a scrip based on http://requesttracker.wikia.com/wiki/OnCreateAddGroupCc, which automatically adds CCs based on a custom field.

Adding the CC works fine, and they get copied on further correspondence. But they don’t get the initial email like they would if I put them in the actual CC box when creating the ticket.
I assume what happens is all the scrips run for the initial ticket, including the my custom one and the one that sends emails to CCs. But even though mine runs first, it still doesn’t see the added CC, since that’s a separate transaction.

I’ve searched for a scrip that would send the original ticket to new CC’s when they are added, but haven’t come up with anything. How can I make sure the new CCs get an email when they are added by my scrip instead of the CC box during ticket creation?

RT version 4.2.0. All other scrips are the default ones, nothing has been changed except adding mine.

In case I’m doing something wrong with my scrip, the code is below.

Thanks,

Grant Emsley

Description: 01 On Create Add Department CCs
Condition: User Defined
Action: User Defined
Template: Blank

Custom Condition:

$RT::Logger->info(“On Create Add Department CCs: entering condition check”); if(
(($self->TransactionObj->Type eq “Create”) || ($self->TransactionObj->Type eq “CustomField”))
&&($self->TicketObj->FirstCustomFieldValue(‘Department’))
&&($self->TicketObj->FirstCustomFieldValue(‘Department’) ne ‘None’)
) {
$RT::Logger->info("On Create Add Department CCs: met conditions - Department is " . $self->TicketObj->FirstCustomFieldValue(‘Department’));
return 1;
} else {
return undef;
}

Custom action preparation code:

my $groupname = ‘CC-’ . $self->TicketObj->FirstCustomFieldValue(‘Department’);

Load the custom group from RT

my $groupObj = RT::Group->new($RT::SystemUser); $groupObj->LoadUserDefinedGroup($groupname);
return undef unless $groupObj;

Instead of adding the group, find and add the members of the group # This way, we can exclude the requestor themselves from being CC’ed.

my $groupMembersObj = $groupObj->UserMembersObj;

$RT::Logger->info(“Finding members of group $groupname for ticket #” . $self->TicketObj->id); my $userObj; while ($userObj = $groupMembersObj->Next) {
if(($self->TicketObj->IsRequestor($userObj->PrincipalId)) or ($self->TicketObj->IsCc($userObj->PrincipalId))) {
$RT::Logger->info("On Create Add Department CCs: Not adding " . $userObj->Name . ", already on ticket " . $self->TicketObj->id);
} else {
$RT::Logger->info("On Create Add Department CCs: Adding " . $userObj->Name . " to ticket " . $self->TicketObj->id);
my ($success, $msg) = $self->TicketObj->AddWatcher(
Type => “Cc”,
PrincipalId => $userObj->PrincipalId);
if(!$success) {
$RT::Logger->info("On Create Add Department CCs: Could not add " . $userObj->Name . " to " . $self->TicketObj->id . "Got: " . $msg);
}
}
}
return 1;

For anyone who comes across this in the future:

Change the notification scripts to run in the Transaction Batch stage
instead of Transaction Create. All the transaction create scrips are
prepared, then they all get committed. The notification scripts decide
who to send the emails to in the preparation step, before the new CC
has actually been committed.

The Transaction Batch stage starts after all the transaction create scrips have been committed, so it will see the new watcher.

You should be aware that changing your notification scrips to
Transaction Batch means that you will not be able to preview scrips
(for people who have ShowOutgoinMail granted and get to see who will
receive mail from the ticket).

-kevin