OnCreateSetUserDetails

From Request Tracker Wiki
Revision as of 18:49, 13 August 2016 by Tharn (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This scrip is a work in progress (it works for me, but is not well-polished). It pulls information from a vCard if present, and uses it to set user details. In particular note that it currently relies upon the presence of an address of type x-rt. For your use you'll probably want that to be work or home, or prefer one and fail over to the other e.g;

-my $adr = $card->get({node_type=>'addresses', types=>'x-rt'})->[0];
+my $adr = $card->get({node_type=>'addresses', types=>'home'})->[0] || $card->get({node_type=>'addresses', types=>'work'})->[0] ;

Scrip Fields:

Description: OnCreate Set user details
Condition: On Create
Action: User Defined
Template: Global Template: Blank
Stage: Transaction Create

Custom Condition:

  # return $Attachment->First->ContentType eq 'text/x-vcard' ? 1 : 0;
 my $Attachments = $self->TransactionObj->Attachments('text/x-vcard');
 return $Attachments->Count() ? 1 : 0;
 
 

Custom action preparation code:

 use Text::vCard::Addressbook;
 my $Transaction = $self->TransactionObj;
 my $body = $Transaction->Attachments('text/x-vcard')->First->Content;
 
 #Parse
 my $card = Text::vCard::Addressbook->new({source_text=>$body})->vcards()->[0];
 return 0 unless $card; #sometimes the condition ->Count() fails :-(
 my $adr = $card->get({node_type=>'addresses', types=>'x-rt'})->[0];
 my %usr = (
           requestor=>$card->email,
           RealName=>$card->fullname,
           Organization=>$card->get({node_type=>'ORG'})->[0]->name,
           Address1=>$adr->street,
           Address2=>$adr->extended,
           City=>$adr->city,
           State=>$adr->region,
           Country=>$adr->country,
           Zip=>$adr->post_code,
          );
 foreach my $tel ( $card->get({node_type=>'TEL'}) ){
    $usr{HomePhone} = $tel->value if $tel->is_type('home');
    $usr{WorkPhone} = $tel->value if $tel->is_type('work');
    $usr{PagerPhone} = $tel->value if $tel->is_type('pager');
    $usr{MobilePhone} = $tel->value if $tel->is_type('cell');
 }
 
 #Tweak requestor with %usr
 my $user = RT::User->new($RT::SystemUser);
 $user->LoadByEmail(delete($usr{requestor}));
 foreach my $attr ( keys %usr){
   my $method='Set' . $attr;
   $user->$method($usr{$attr});
 }
 
 

Custom action cleanup code:

1;