Problem with getting custom fields

Hi

I create new Queue in this queue i have some cutumFileds.

Problem is that I can not access to them via api when I set them via www.

Can be anything related with my api code?

my $url = 'http://172.31.1.121:8888/tkchangestatus';
my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
my $data = {
status => $self->TicketObj->Status, 
tkticketid => $self->TicketObj->FirstCustomFieldValue("TelefonicaID"),
resolutioncategory=> $self->TicketObj->FirstCustomFieldValue("ResolutionCategory"),
resolutioncategory2=> $self->TicketObj->FirstCustomFieldValue("ResolutionCategory2"),
resolutioncategory3 => $self->TicketObj->FirstCustomFieldValue("ResolutionCategory3"),
rfo=> $self->TicketObj->Comment
};

RT::Logger->error("resolutioncategory:", $self->TicketObj->FirstCustomFieldValue("ResolutionCategory"));
RT::Logger->error("data:",  $data);
my $attachments = $self->TransactionObj->Attachments;
my $content;

my $c=0;
my %hash =();

while (my $attach = $attachments->Next) {   

        RT::Logger->error("attach:",  $content);
        RT::Logger->error("attachment type:", $attach->ContentType());
        RT::Logger->error("attachment in while:",  $attach);
        $content = $attach->Content();
        RT::Logger->error("content in while:",  $content);
        RT::Logger->error("counter:",  $c);
        $hash{"Tickets"}{"content"}=$content;
        $hash{"Tickets"}{"contentType"}=$attach->ContentType();
        $c++;
}



$hash{status} = $data;
RT::Logger->error("hash:" . Dumper(\%hash));
my $encoded_data = encode_json(\%hash);

Thank you!

Hi

what i noticed is this. If go to the ticket and then to Actions (top menu, the last option) and when dropdown opens you select resolved (or comment,…).

And then you add comment, add right CF and you do update of the ticket. Then this values are not send via api.

Is there any way to resolve this?

br
miha

Is this code in a RT::Scrip or a standalone Perl script?

Hi Craig

yes, this is RT::Scrip, which i have entered via www.

For my understanding could you explain a little of what is it you want this script to do?

You might want to look at the LoadCustomFieldByIdentifier() in RT::Ticket as that is overloaded to look at queue specific custom fields before global ones.

@craig . I need to send status of the ticket, and need to send all my CFs and the comment/content of the ticket via API (json). If the CF are set before I go to Actions->Resolved that I will get them via API (only comment is missing).

This is log from syslog:

Jun 4 14:44:45 localhost RT: [1361] hash:$VAR1 = {#012 ‘status’ => {#012 ‘No message attached’ => undef,#012 ‘tkticketid’ => undef,#012 ‘rfo’ => 0,#012
‘status’ => ‘open’,#012 ‘resolutioncategory’ => undef,#012 ‘resolutioncategory2’ => undef,#012 ‘resolutioncategory3’ => undef#012 }#012 };

If I will do status change again, I will get this values. If the values are changed with status, I will get old values but not the new ones.

@GreenJimll
Like this i guess. But I get hash, i tried with dump hash but I do not see any useful data.
RT::Logger->error(“Testing RC”, $self->TicketObj->LoadCustomFieldByIdentifier(“ResolutionCategory”));

The hash is the RT::CustomField object that has been returned.

@GreenJimll I not it is noob question but I am so new to perl.

Ok I get an object.
$CFrc1=$self->TicketObj->LoadCustomFieldByIdentifier(“ResolutionCategory”)
Which method I need now to use to get values. Or in easier way, how to use the right method to get data?

also what about comment, which is also not displayed?

What is the condition that your scrip uses?

When you get ‘undef’ for the custom field values does the ticket have a value for these fields already and they are not showing up? Or are the field values being set on the action that you are performing (IE the values are being set but not set while the scrip runs)?

Going back to the “what the scrip should do” how accurate is this “On ticket comment or reply send the comment and some other relevant fields to some external API”?

@Craig, the second thing. The field values are being set on the action that I am preforming (The values are set as I can see them but the script does see them).

if I set status again (different) and also change CF I can see old CF, but not new ones.

For exp now i changed status and add CF:

  • Ticket 219: Status changed from ‘open’ to ‘resolved’
  • ResolutionCategory Others added
  • CircuitID asd added
  • TelefonicaID asdasd added
  • ResolutionCategory3 Natural phenomenon added
  • ResolutionCategory2 (O) Others added

log in syslog:
hash:$VAR1 = {#012 ‘status’ => {#012 ‘resolutioncategory2’ => undef,#012 ‘resolutioncategory3’ => undef,#012 ‘status’ => ‘resolved’,#012 ‘resolutioncategory’ => undef,#012 ‘No message attached’ => undef,#012 ‘tkticketid’ => undef,#012 ‘rfo’ => 0#012
}#012 };
Jun 4 15:11:04 localhost RT: [1357] status: resolved

And now If i change status again with new CF.

  • Ticket 219: Status changed from ‘resolved’ to ‘open’
  • ResolutionCategory3 Natural phenomenon changed to Improper termination through service modification
  • ResolutionCategory2 (O) Others changed to § Improper Terminations
  • ResolutionCategory Others changed to Provider

you see old values for CF:

hash:$VAR1 = {#012 ‘status’ => {#012 ‘status’ => ‘open’,#012 ‘resolutioncategory’ => ‘Others’,#012 ‘resolutioncategory3’ => ‘Natural phenomenon’,#012 ‘resolutioncategory2’ => ‘(O) Others’,#012 ‘rfo’ => 0,#012 ‘tkticketid’ => ‘asdasd’,#012 ‘No message attached’ => undef#012 }#012 };
Jun 4 15:13:00 localhost RT: [1359] status: open

the last question:
I have set condition on “On Status Change”

For me it looks like script is executed before all values are set.

Since RT scrips run per transaction it seems that status gets set before the custom field values, meaning if you run this code then you will see old values for custom fields.

It may be better to use a callback from the ticket display page and in that callback get the Ticket object and send the values of the object to your API.

You can use the ‘BeforeDisplay’ callback from Ticket/Display.html you will need a method for checking that an update occurred (Maybe check if the @Actions array has any values) and if so then you can send the data to your API.

What happens if you set the scrip to run as batch?

Good catch! Thanks jeff

@Jeff_Voskamp2 and @craig thank you! As I selected to run script as batch the script started to work properly.

Now when I select script an select from normal to bacth, when I can see that this script runs as batch :)?
Just to know how to find out in future in which stage some script belongs to :slight_smile:

tnx!

OK, i think final thing.

This is one json exp that i get:

{"Tickets":{"content":"asdsadd","contentType":"text/html"},"status":{"tkticketid":"TK123123213","resolutioncategory2":"(C) Customer Premise Equipment","status":"resolved","resolutioncategory3":"Activation /routine configuration error","resolutioncategory":"Customer","No message attached":null,"rfo":0}}

I do not see from where in code the key: “No message attached” is generated.

code.

use strict;
use warnings;
use LWP::UserAgent;
 
use Encode qw(encode_utf8);
use HTTP::Request();
use JSON::MaybeXS qw(encode_json);


my $ua = LWP::UserAgent->new;


my $url = 'http://172.31.1.121:8888/tkchangestatus';
my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
my $data = {
status => $self->TicketObj->Status, 
tkticketid => $self->TicketObj->FirstCustomFieldValue("TelefonicaID"),
resolutioncategory=> $self->TicketObj->FirstCustomFieldValue("ResolutionCategory"),
resolutioncategory2=> $self->TicketObj->FirstCustomFieldValue("ResolutionCategory2"),
resolutioncategory3 => $self->TicketObj->FirstCustomFieldValue("ResolutionCategory3"),
rfo=> $self->TicketObj->Comment
};


my $attachments = $self->TransactionObj->Attachments;
my $content;

my $c=0;
my %hash =();
while (my $attach = $attachments->Next) {   

        RT::Logger->error("attach:",  $content);
        RT::Logger->error("attachment type:", $attach->ContentType());
        RT::Logger->error("attachment in while:",  $attach);
        $content = $attach->Content();
        RT::Logger->error("content in while:",  $content);
        RT::Logger->error("counter:",  $c);
        $hash{"Tickets"}{"content"}=$content;
        $hash{"Tickets"}{"contentType"}=$attach->ContentType();
        $c++;
}


$hash{status} = $data;
my $encoded_data = encode_json(\%hash);
 
my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
RT::Logger->error("status: ", $self->TicketObj->Status, "\n");


my $resp = $ua->request($r);
if ($resp->is_success) {
    my $message = $resp->decoded_content;
    RT::Logger->debug("Received reply: $message\n");
}
else {
    RT::Logger->error("HTTP GET error code: ", $resp->code, "\n");
    RT::Logger->error("HTTP GET error message: ", $resp->message, "\n");
    return 0;
}


return 1;

Probably from:

rfo=> $self->TicketObj->Comment

as that’s a method call to add a comment to a ticket, and you’ve not provided any message to add.