OnCreateAddGroupCc: Difference between revisions

From Request Tracker Wiki
Jump to navigation Jump to search
m (2 revisions imported)
(No difference)

Revision as of 15:15, 6 April 2016

On Create Add Group Cc

I needed to add several CCs at an OEM partner whenever someone from the partner opened a ticket. It would be problematic if I skipped the entire group because one of them was already a CC or requestor, so I reworked the OnCreateSetDeptHeadCc scrip to eliminate the bug mentioned therein. The solution was to refactor so that the main loop iterates through the members of the group to add, and adds them if they are not already CCs or Requestors -- instead of iterating through the CC and Requestor lists to see if any of the principals are members of the group to add.

  • Description: ''01 On Create Add Group CCs''
  • Condition: ''On Create''
  • Action: ''User Defined''
  • Template: ''Global template: Blank''
  • Stage: ''TransactionCreate''
  • Custom condition: (blank)
  • Custom action preparation code:
$RT::Logger->debug("On Create Add Group CCs: entering custom action prep");
 my $ticket = $self->TicketObj;
 my $transaction = $self->TransactionObj;
 my $derivedGroupName = 'partner-OEM-CClist';
 
 # match any ticket created with a Requestor list that includes @*oem.com addresses
 my $OEMregex = '(?i).*@oem.com[,$]|.*@[\w\-]\.oem.com[,$]';
 if ($ticket->RequestorAddresses =~ m/$OEMregex/) {
     $RT::Logger->debug("On Create Add Group CC: matched an OEM address in Requestor list");
 } else {
     $RT::Logger->debug("On Create Add Group CC: no match: '" . $ticket->RequestorAddresses . "'");
     return undef;
 }
 
 # instantiate a group object
 my $addGroupObj = RT::Group->new($RT::SystemUser);
 $addGroupObj->LoadUserDefinedGroup($derivedGroupName);
 return undef unless $addGroupObj;
 my $addGroupMembersObj = $addGroupObj->UserMembersObj;
 
 my $userObj;
 # walk through members of group to add; if a given member is not already on the ticket, add to the CC list
 while ($userObj = $addGroupMembersObj->Next) {
     if (($ticket->IsRequestor($userObj->PrincipalId)) or ($ticket->IsCc($userObj->PrincipalId))) {
         $RT::Logger->debug("On Create Add Group CC: '" . $userObj->Name . "' is already a ticket watcher; not adding Cc on ticket \#" . $ticket->id );
     } else {
 	$RT::Logger->debug("On Create Add Group CC: Adding '" . $userObj->Name ."' to ticket \#" .$ticket->id);
 	my ($success, $msg)= $ticket->AddWatcher(
 						  Type => "Cc",
 						  PrincipalId => $userObj->PrincipalId);
 	if (! $success) {
 	    $RT::Logger->info("On Create Add Group CC: couldn't add '" . $userObj->Name . "' to " . $ticket->id . "': got '" . $msg ."'");
 	}
     }
 }
 return 1;
 
 
 
  • Custom action cleanup code: (blank)

Ole Craig Fri Sep 14 17:19:00 MDT 2007

RT 3.8.6

Worked as is - great recipe. Thanks for sharing.