site logo

Example: Ticket search and listing (Perl)


Example: ticket search and listing in Perl  

This code produces a list of tickets selected based on some criteria such as ticket status and creation date.

#!/usr/bin/perl
#
# This script demonstrates the Mojo Helpdesk Ticket API (SOAP access).

#
# You will need to adjust the BASE_URL and ACCESS_KEY values:
#
# BASE_URL - Base URL to your helpdesk.
#
# ACCESS_KEY - Your access key, as displayed on your "profile" page.
#

use strict;
use warnings;
use SOAP::Lite;

#
# Configuration settings.
#
#my $BASE_URL = 'http://mycompany.mojohelpdesk.com';
my $BASE_URL = 'http://mycompany.mojohelpdesk.com';
my $ACCESS_KEY = 'ec91417dddfd7jhgkjhgkjhgjkhg53dce59058932';

#
# Create the SOAP client.
#
my $client = SOAP::Lite->new(proxy => "$BASE_URL/backend/api");

#
# Construct the search query. Some examples are shown.
#
my $search = bless {

### # open tickets assigned to me, changed in the past 5 days
### "status" => "open",
### "assigned_to_id" => "me",
### "updated_days" => "5",
### "updated_days_before_or_after" => "after",

# open tickets created more than a week ago
"status" => "open",
"created_days" => "7",
"created_days_before_or_after" => "before",

### # normally results are limited to 10 tickets
### # set zero for unlimited results
### "results_per_page" => 0,
"results_per_page" => 0,

} => 'TicketSearchParameters';

#
# Execute the remote query.
#
my $resp = $client->FindTickets($ACCESS_KEY, $search);
die "SOAP protocol failure: $resp->faultstring\n" if $resp->fault;

#
# Result is a TicketListResponse object.
#
my $result = $resp->result;
die "API query failure: $result->{error_message}\n" unless $result->{successful};

#
# Process the results.
#
print "$result->{number_tickets} tickets found.\n";
foreach my $ticket (@{$result->{tickets}}) {
print
"Ticket: ", $ticket->{id}, "\n",
"Title: ", $ticket->{title}, "\n",
"Status: ", $ticket->{status}, "\n",
"Created by: ", $ticket->{created_by}->{full_name}, "\n",
"Company name: ", $ticket->{company}->{name}, "\n",
"Company id: ", $ticket->{company}->{id}, "\n",
"URL: ", $ticket->{url}, "\n",
"\n";
}