Custom Fields and Templates

I’d like to print out the custom fields and their values in a
template. I’m just trying to get it to print out the CF name first
and I can’t even get that far.

{
my $CustomFields = $TicketObj->QueueObj->CustomFields();
while (my $CustomField = $CustomFields->Next()) {
$OUT .= $CustomField->Name . “\n”;
}
$OUT;
}

It quits with the following error:

Program fragment delivered error ``Can’t call method “QueueObj” on an undefined value at template line 15.
(line 15 is the line with QueueObj in it.)

Stack:
[template:15]
[/usr/local/rt3/lib/RT/Template_Overlay.pm:395]
[/usr/local/rt3/lib/RT/Template_Overlay.pm:315]
[/usr/local/rt3/lib/RT/Action/SendEmail.pm:210]
[/usr/local/rt3/lib/RT/ScripAction_Overlay.pm:199]
[/usr/local/rt3/lib/RT/Scrip_Overlay.pm:402]
[/usr/local/rt3/lib/RT/Scrip_Overlay.pm:345]
[/usr/local/rt3/lib/RT/Scrips_Overlay.pm:196]
[/usr/local/rt3/lib/RT/Transaction_Overlay.pm:118]
[/usr/local/rt3/lib/RT/Ticket_Overlay.pm:3801]
[/usr/local/rt3/lib/RT/Ticket_Overlay.pm:3944]
[/usr/local/rt3/lib/RT/Ticket_Overlay.pm:3318]
[/usr/local/rt3/lib/RT/Interface/Web.pm:949]
[/usr/local/rt3/lib/RT/Interface/Web.pm:1071]
[/usr/local/rt3/share/html/Ticket/Display.html:108]
[/usr/local/rt3/share/html/Ticket/Update.html:196]
[/usr/local/rt3/share/html/autohandler:189]’'
Andy Harrison
(full headers for details)

Andy Harrison wrote:

I’d like to print out the custom fields and their values in a
template. I’m just trying to get it to print out the CF name first
and I can’t even get that far.

{
my $CustomFields = $TicketObj->QueueObj->CustomFields();
while (my $CustomField = $CustomFields->Next()) {
$OUT .= $CustomField->Name . “\n”;
}
$OUT;
}

In template code you have $Ticket not $TicketObj.

	Good luck. Ruslan.

REPLY TO LIST TOO.
I’m not a BestPractical’s support team member. :slight_smile:

{
my $CustomFields = $Ticket->QueueObj->CustomFields();
while (my $CustomField = $CustomFields->Next()) {
my $CustomFieldValues=$Ticket->CustomFieldValues($CustomField->Id);

  if ($CustomFieldValues->Count) {
    $OUT .= $CustomField->Name . ":\n";
  } else {
    next;
  }

  while (my $CustomFieldValue = $CustomFieldValues->Next) {
    $OUT .= "\t" . $CustomFieldValue->Content . "\n";
  }
}
$OUT;

}

What do you think about this??? :slight_smile:
Best regards. Ruslan.
Andy Harrison wrote:

REPLY TO LIST TOO.
I’m not a BestPractical’s support team member. :slight_smile:

Oops, that pesky Reply All button…

{
my $CustomFields = $Ticket->QueueObj->CustomFields();
while (my $CustomField = $CustomFields->Next()) {
my $CustomFieldValues=$Ticket->CustomFieldValues($CustomField->Id);

  if ($CustomFieldValues->Count) {
    $OUT .= $CustomField->Name . ":\n";
  } else {
    next;
  }

  while (my $CustomFieldValue = $CustomFieldValues->Next) {
    $OUT .= "\t" . $CustomFieldValue->Content . "\n";
  }
}
$OUT;

}

What do you think about this??? :slight_smile:
Best regards. Ruslan.

Thanx a lot. Works like a champ.

Here’s my slightly modified finished product:

{
my $CustomFields = $Ticket->QueueObj->CustomFields();
while (my $CustomField = $CustomFields->Next()) {
my $CustomFieldValues=$Ticket->CustomFieldValues($CustomField->Id);
$OUT .= $CustomField->Name;
if ($CustomFieldValues->Count) {
$OUT .= “:” . " " x (20 - length($CustomField->Name));
} else {
$OUT .= “:\n”;
next;
}
while (my $CustomFieldValue = $CustomFieldValues->Next) {
$OUT .= $CustomFieldValue->Content . “\n”;
}
}
$OUT;
}

The hardcoded 20 for the length isn’t very stylish, but I didn’t feel
like bothering doing it right. Future individuals who try this snippet
will want to make sure it doesn’t bite you if you have long CF names.

Andy Harrison
(full headers for details)

You’ve forgot about custom fields with multiply values. It break your
indend, but not mine :slight_smile:
Second ‘while’ specialy for this case.

	Good luck. Ruslan.

Andy Harrison wrote:

You’ve forgot about custom fields with multiply values. It break your
indend, but not mine :slight_smile:
Second ‘while’ specialy for this case.

Oh yes. Here’s a prettier version. Multiple value fields are
displayed underneath the fieldname instead of beside it.

{
my $CustomFields = $Ticket->QueueObj->CustomFields();
while (my $CustomField = $CustomFields->Next()) {
my $CustomFieldValues=$Ticket->CustomFieldValues($CustomField->Id);
$OUT .= $CustomField->Name;
if ($CustomFieldValues->Count) {
my $spacer;
if ( $CustomField->Type ne ‘FreeformMultiple’ and
$CustomField->Type ne ‘SelectMultiple’ ) {
$spacer = " " x (20 - length($CustomField->Name));
} else {
$spacer = “\n”;
}
$OUT .= “:” . $spacer;
} else {
$OUT .= “:\n”;
next;
}
while (my $CustomFieldValue = $CustomFieldValues->Next) {
$OUT .= " " if ( $CustomField->Type eq ‘FreeformMultiple’ or
$CustomField->Type eq ‘SelectMultiple’ );
$OUT .= $CustomFieldValue->Content . “\n”;
}
$OUT .= “\n” if ( $CustomField->Type eq ‘FreeformMultiple’ or
$CustomField->Type eq ‘SelectMultiple’ );
}
$OUT;
}

Andy Harrison
(full headers for details)

Andy Harrison wrote:

Subject: “Re: [rt-users] Custom Fields and Templates”



> You've forgot about custom fields with multiply values. It break your 
> indend, but not mine :)
> Second 'while' specialy for this case.


Oh yes.  Here's a prettier version.  Multiple value fields are
displayed underneath the fieldname instead of beside it.

{
  my $CustomFields = $Ticket->QueueObj->CustomFields();
  while (my $CustomField = $CustomFields->Next()) {
    my $CustomFieldValues=$Ticket->CustomFieldValues($CustomField->Id);
    $OUT .= $CustomField->Name;
    if ($CustomFieldValues->Count) {
      my $spacer;
      if ( $CustomField->Type ne 'FreeformMultiple' and
           $CustomField->Type ne 'SelectMultiple' ) {
        $spacer = " " x (20 - length($CustomField->Name));
      } else {
        $spacer = "\n";
      }
      $OUT .= ":" . $spacer; 
    } else {
      $OUT .= ":\n";
      next;
    }
    while (my $CustomFieldValue = $CustomFieldValues->Next) {
      $OUT .= " " if ( $CustomField->Type eq 'FreeformMultiple' or
                        $CustomField->Type eq 'SelectMultiple' );
      $OUT .= $CustomFieldValue->Content . "\n";
    }
    $OUT .= "\n" if ( $CustomField->Type eq 'FreeformMultiple' or
                        $CustomField->Type eq 'SelectMultiple' );
  }
  $OUT;
 }


I’ve copied it to wiki.bestpractical.com

	Best regards. Ruslan.