CodeSnippets: Difference between revisions
Jump to navigation
Jump to search
(→Introduction: Major grammar/wording updates.) |
m (→Some code that is still here and has no its own place: Adjusted section title) |
||
Line 7: | Line 7: | ||
* [[TemplateSnippets]] - by default in RT, [[Template]]s are used only in notification actions. In such templates, you can insert information about tickets, users, queues, transactions, and so on into outgoing emails. No changes are allowed in templates; it's all about getting data. [[CustomConditionSnippets]] may be interesting as well. | * [[TemplateSnippets]] - by default in RT, [[Template]]s are used only in notification actions. In such templates, you can insert information about tickets, users, queues, transactions, and so on into outgoing emails. No changes are allowed in templates; it's all about getting data. [[CustomConditionSnippets]] may be interesting as well. | ||
= | = Miscellaneous Snippets = | ||
== Groups and users == | == Groups and users == | ||
Line 23: | Line 23: | ||
Load a user from | Load a user from the database | ||
<nowiki>my $user = RT::User->new( $RT::SystemUser ); | <nowiki>my $user = RT::User->new( $RT::SystemUser ); |
Latest revision as of 15:38, 13 January 2017
Introduction
A Code snippet is a bit of Perl code that you can use in many places to customize RT. Scrips and templates both use Perl code for most of what they do, so I've put together some pages of Perl snippets. organized by type.
- CustomConditionSnippets - snippets that you'd usually use in ScripConditions. As conditions shouldn't change anything about tickets or users, you'll find only code to retrieve information here. This is similar to TemplateSnippets.
- CustomActionSnippets - ScripActions change tickets, so here is where you'll find how to modify RT objects.
- TemplateSnippets - by default in RT, Templates are used only in notification actions. In such templates, you can insert information about tickets, users, queues, transactions, and so on into outgoing emails. No changes are allowed in templates; it's all about getting data. CustomConditionSnippets may be interesting as well.
Miscellaneous Snippets
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 the database
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;