Query Ticket Custom Fields via API

Hi, I'm looking to retrieve all tickets which have a custom field marked with a specific value via the API.

For example, we have a 'Location' custom field which has values such as 'Bucket A' and so on... I want to return all tickets which have 'Bucket A' selected as the location. I've managed to grab this info about the custom field:

{"id":18,"type":"CustomFieldDefinition","label":"Location","optionsString":";Goods Out;Stock;Under Counter;Bay 1;Bay 2;Bay 3;Bay 4;Bay 5;Bay 6;Bucket A;Bucket B;Bucket C;Bucket D;Bucket E;Bucket F;Bucket G","displayStyleStorableValue":3,"visible":true,"editable":true,"required":false}

Any help would be greatly appreciated!

Cheers.

  • I can send you some code I've used to do this both in a straight MYSQL search or using the API.  Which would you want?

  • I've put together a python script now, but I'm always interested in seeing how someone else has done it, perhaps I can improve on mine. Seeing your API query would be nice, cheers.

  • This is my code for PHP.  You probably already figured this out with your python script, but basically it's just a matter of 

    $ch_ticket_string = "">helpdesk/.../" . $ticket_id . "?username=hrpaf&apiKey=xxxxxxxXOQJdN6xxxxxehshQWi64NAUxxxxxxgW";
    $ch_ticket = curl_init($ch_ticket_string);
    curl_setopt($ch_ticket, CURLOPT_RETURNTRANSFER, true);

    $curl_result_ticket = curl_exec($ch_ticket);


    /* Populate Ticket Retrieved Ticket Record now */
    $json_ticket = json_decode($curl_result_ticket,true);
    $ticket_id = $json_ticket['id'];
    $custom_field1 = $json_ticket['ticketCustomfields']['type'];
    $selected_ticket_json = curl_get("/ra/Tickets/mine?style=long");
    $selected_ticket = json_decode($selected_ticket_json);

    $department_id = $json_ticket['department']['id'];
    $location_id = $json_ticket['location']['id'];
    $client_id = $json_ticket['clientReporter']['id'];
    $report_to = $json_ticket['clientReporter']['lastName'] . "," . $json_ticket['clientReporter']['firstName'];
    $datetime=date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));


    foreach ($selected_ticket as $ticket)  //Loop through each ticket
    {


    $found_ticket_id = $ticket->id;

    If ($found_ticket_id == $ticket_id)
    {


    $custom_fields = $ticket->ticketCustomFields;  // For each ticket you find assign ticketCustomFields array


    foreach ($custom_fields as $field)  // Loop through each of the ticketCustomFields
    {


    $custom_field_def_id = $field->definitionId;  // grab the definition ID of the custom field


    switch ($custom_field_def_id)  // check each definition ID 
    {
    case 16;
    $first_name = $field->restValue;
    break;

    case 17;
    $middle_init = $field->restValue;
    break;

    case 18;
    $last_name = $field->restValue;
    break;

    case 3;
    $emp_id = $field->restValue;
    break;

    case 357;
    $wtr_date = $field->restValue;
    break;

    } //switch

    } // foreach $custom_fields
    break;


    } // if $found_ticket_id


    } // foreach $selected_ticket