Difference between revisions of "OnCreateSetUserDetails"

From Request Tracker Wiki
Jump to navigation Jump to search
m (2 revisions imported)
 
 
Line 16: Line 16:
Custom Condition:
Custom Condition:


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


Custom action preparation code:
Custom action preparation code:


  <nowiki>use Text::vCard::Addressbook;
  <pre>
use Text::vCard::Addressbook;
  my $Transaction = $self-&gt;TransactionObj;
  my $Transaction = $self-&gt;TransactionObj;
  my $body = $Transaction-&gt;Attachments('text/x-vcard')-&gt;First-&gt;Content;
  my $body = $Transaction-&gt;Attachments('text/x-vcard')-&gt;First-&gt;Content;
Line 58: Line 60:
  }
  }
   
   
  </nowiki>
  </pre>


Custom action cleanup code:
Custom action cleanup code:


  1;
  1;

Latest revision as of 18:49, 13 August 2016

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;