site logo

Example: Ticket with custom fields (Ruby)


Example: working w/ ticket w/ custom fields (Ruby)  

This script shows how to:

  • connect to your helpdesk API with your access key
  • create a ticket with custom fields

 

#!/usr/bin/ruby
########################################
# $Id: create-ticket-with-custom-fields.rb,v 1.1 2007/08/15 17:04:25 d Exp $
#
# This is an example how to use the Mojo Helpdesk API
# Language: ruby
#
# This script shows how to:
# - create a ticket that has CUSTOM FIELDS.
#
# You will need to adjust the URL and MYID values
# and *** also *** the custom field names.
#
# URL - Base URL to your helpdesk.
#
# MYID - Your access key, as displayed on your "profile" page
#
########################################
# Feel free to use this code and modify to fit your needs.
# More information visit www.mojohelpdesk.com
########################################
require 'soap/wsdlDriver'
require 'open-uri'

########################################
# Your helpdesk URL
# URL = "http://supportxyz.mojohelpdeskcom
# MYID = "8b1459cfcb367d72419e2642e56fa3b72bc5c2a0"
#
# Replace the info below with your own:
########################################
URL = "http://yourhd.mojohelpdesk.com"
MYID = "find-your-key-in-your-user-profile"
########################################

begin
drv = SOAP::WSDLDriverFactory.new("#{URL}/backend/service.wsdl").create_rpc_driver
rescue
printf "Fatal error: Could not connect to server.wsdl or an error occured (%s). Check your URL and try again.\n", $!; exit(-1)
end

printf ">>> WSDL found.\n"

#
# Ticket creation
#
# Note: you NEED to change the custom_fields with ticket customer field. ('i.e. change 'ref' with yours)
# if you have many custom fields do something like this:
# 'custom_fields' => [{:field_name => 'ref', :field_
puts ">>> Updating ticket"
t['title'] = " MODIFIED TITLE "
t['ticket_id'] = t['id']
res2 = drv.UpdateTicket("#{MYID}", t)
if res2['successful'] == true
puts res2.inspect
else
printf ">>> ticket update failed\n"
end


params = {
"user_id" => "1",
"title" => 'test ticket',
"description" => 'desc',
"ticket_queue_id" => '1',
"comment" => 'This is a commment at creation',
'priority_id'=>'40',
'custom_fields' => [{:field_name => 'ref', :field_value =>'123'}]
}

result = drv.CreateTicket("#{MYID}", params)

if result['successful'] != true
printf ">>> Error: %s\n", result['error_message']
exit(-1)
end

t = result.ticket
printf ">>> Ticket created, id: %d,title:\%sn",t['id'], t['title']
puts t.custom_fields.inspect
t.custom_fields.each{|f|
printf " custom field: %s: %s\n", f.field_name, f.field_value
}



exit(0)