List all transactions from an update in template

Novice user here - novice to both RT and perl.

If I understand correctly, when updates to multiple fields are made during a single transaction, the individual actions are batched together and can be accessed in scrips using

TicketObj->transactions

I would like to be list all of the transactions in an update in an template. I have tried a few different versions of something like this, to no avail:

{
my $txns = $self->TicketObj->Transactions;
foreach $txn ($txns) {
$txn->Description;
};
}

Try something like:

my $txns = $self->TicketObj->Transactions();
while(my $txn = $txns->Next) {
   my $doSomethingWith = $txn->Description;
   # Whatever you want to do in here.
}
1 Like

Thank you for the reply. Unfortunately, I don’t currently have access to the logs, so I am shooting in the dark somewhat.

The $self->TicketObj->Transactions(); syntax seemed to be crashing (not sending a message at all).

I did get the following to not crash (i.e. it still sends the email). However, the “loop” line is not printing. So I think the $txns object is empty.

{
   my $txns = $Ticket->Transactions();
   while(my $txn = $txns->Next) {
     "Loop"
   }
}

I think you’d need to set an out variable like:

{
   my $txns = $Ticket->Transactions();
   while(my $txn = $txns->Next) {
     $OUT = "Loop";
   }
}
1 Like
{
   my $txns = $Ticket->Transactions();
   while(my $txn = $txns->Next) {
     $OUT = $txn->Description;
   }
}

Ok. That does give me an output from the loop. Unfortunately, it is just a single transaction. It is a different transaction from what I get when I use {$Transaction->Description} outside of the loop, so I think that is encouraging. But it doesn’t seem to iterate through all.

Also, can you take a moment to explain to me how assigning it to a variable caused it to be included in the message text? In other languages, I would have needed to use a print statement or something similar to get an output for something contained inside of the code block delimiters. Is $OUT some type of special variable?

I think you want

$self->TransactionObj

And loop over that collection.

See this for $OUT

Also for getting all the transactions you’ll want to concatenate $OUT

$OUT .= $txn->Description

1 Like

Thank you! The link to info on $OUT was very helpful.

I think we are almost there!

Changing for concatenation did the trick as far as getting the full list of transactions. And by adding a new line to the end, it does come out as a list:

     $OUT .= $txn->Description."\n";

With my original, I am getting all of the transaction history of the ticket - not just the latest batched changes:

   my $txns = $Ticket->Transactions();

I tried the following variations of your recommendation:

   my $txns = $self->TransactionObj;
   my $txns = $self->TransactionObj->Transactions;
   my $txns = $self->TransactionObj();
   my $txns = $self->TransactionObj->Transactions();

But all of those seem to crash (no message sent at all). I’m guessing there is some syntax that I am missing.

What version of RT btw? If you’re new enough you can get the scrip logs in the UI

1 Like

Umm I just realised: you might not have a $self defined. That could be why you’re seeing crashes. $self is often used in scrip condition and action code, but you probably want to be looking here for Perl based RT template variables: Customizing/Templates - RT 5.0.7 Documentation - Best Practical

1 Like

:person_facepalming: good point @GreenJimll

1 Like

Oh, that would be cool! We are one 5.0.4, unfortunately. Let me dig into what @GreenJimll just shared.

I can probably get access to the logs if it comes to that. I am hoping to brute-force my way into something that works - if I can. I do really appreciate all of the assistance.

There is this:

TransactionBatch

Returns an array reference of all transactions created on this ticket during this ticket object’s lifetime or since last application of a batch, or undef if there were none.

Only works when the UseTransactionBatch config option is set to true.

I don’t know if UseTransactionBatch is set to true or not. Since it mentions the return is an array reference, I tried this syntax, but to no avail:

{
  my @txns = $Ticket->TransactionBatch;
  foreach my $txn (@txns)
  {
     $OUT .= $txn->Description."\n";
  }
}

I also don’t have any experience working with batches in this way, but as far as your syntax, you have to modify a bit because it’s an array reference, instead of an array.

You can dereference that result, in order to assign it to an array variable:

my @txns = @{ $Ticket->TransactionBatch };

Alternately, assign it to a scalar variable. so instead of @txns, you’d have $txns. Then dereference that variable when you need it to actually be an array:

{
  my $txns = $Ticket->TransactionBatch;
  foreach my $txn (@$txns)
  {
     $OUT .= $txn->Description."\n";
  }
}
1 Like

@Bradley_Bell
That worked!

@knation @Bradley_Bell @GreenJimll

Thank you all for your help! It now does exactly what I was hoping, spits out a list of all of the changes from the last transaction batch! I had to mark a single response as the solution, but it was a collaborative effort. :slight_smile: Thank you.

Here is my final code:

{
  my @txns = @{ $Ticket->TransactionBatch };
  foreach my $txn (@txns)
  {
     $OUT .= $txn->Description."\n";
  }
}

1 Like