CodeSnippets

From Request Tracker Wiki
Revision as of 15:03, 6 April 2016 by Admin (talk | contribs) (2 revisions imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

Code snippet is a perl code that you can use in many places to customize RT.

Here was one page with code snippets, but now there are many splitted by place where you usually use them.

  • CustomConditionSnippets - snippets that you're usually use in ScripConditions. As conditions shouldn't change anything so you wouldn't find there code to change propetiess of objects, but only code to retrieve information. This is similar to TemplateSnippets and you can reuse information.
  • CustomActionSnippets - ScripActions change tickets and there you'll find info about changes.
  • TemplateSnippets - by default in RT Templates are used only in notification actions and in such templates you can use code to put info about ticket into outgoing email. No changes are allowed in templates, so it's all about getting data and CustomConditionSnippets may be interesting as well.

Some code that is still here and has no its own place

Groups and users

Create a user

my $user = RT::User->new($RT::SystemUser);
my ($id) = $user->Create(
    Name         => 'my_user_login',
    Password     => 'secret',
    RealName     => 'Jhon Smith',
    EmailAddress => 'jhon.smith@example.com',
    Privileged   => 0,
);

Load a user from DB

my $user = RT::User->new( $RT::SystemUser );
 
 $user->Load( 'my_user_login' );
 # or
 $user->Load( 'my_user_id' );
 # or
 $user->LoadByEmail( 'my_user_email_address' );
 
 die "couldn't load user" unless $user->id;
 
 

Load user defined(public) group

my $group = RT::Group->new( $RT::SystemUser );
$group->LoadUserDefinedGroup( 'my group name' );
die "couldn't load group" unless $group->id;

List all user members of the group

my $users = $group->UserMembersObj;
while ( my $user = $users->Next ) {
    print $user->Name, "\n";
}

Add a user to a group

# see earlier how to load a user and a group
 my ($status, $msg) = $group->AddMember( $user->id );
 die "problem $msg" unless $status;