Duplicated tickets while creating a new ticket using the API

Hi!

I’m new to Freshdesk and had to develop a connection between a contact form and Freshdesk using the API. I managed to create a new ticket within Freshdesk, but on each request I get a duplicated ticket in the portal. As far as I can see the request is only triggered once. This is my input for creating a new ticket:

            $formData = [
                'type' => $type,
                'name' => $name,
                'email' => $email,
                'phone' => $phone,
                'subject' => $subject,
                'description' => $description,
                'source' => 1,
                'priority' => 1,
                'status' => 2, 
                'custom_fields' => [
                    'cf_locatie' => $branch
                ]
            ];

Is there something missing in my input or could it be a setting in Freshdesk that I’m not aware of?

Hi @Nick_Kuster ,

Welcome to Freshworks Developer Community!! :bouquet:

Could you please share the sample request/cURL that you are using this API call?

Regards,
Thakur

Hi Thakur,

Thanks!
This is my request:

$data = json_encode($formData);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_USERPWD, env('FRESHDESK_API_KEY'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_URL, env('FRESHDESK_DOMAIN') . $category);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        if (curl_exec($ch) !== false) {
            $result = curl_exec($ch);
        } else {
            throw new MethodNotAllowedException(curl_error($ch));
        }
        curl_close($ch);

        $response = json_decode($result);

The value of CURLOPT_URL is https://mydomain.freshdesk.com/api/v2/tickets

@Nick_Kuster thanks for sharing the details.

There can be a few reasons why this can occur:

  1. This can happen if the first HTTP request is receiving an error and the request is getting re-tried because maxAttempts is set to 2 .
  2. Something else might be triggering the event handler at the same time. This can happen if the same function is exposed as an SMI and some other part of the app (another function) is calling it.
  3. Some other function is triggering the same request.

Could you please check to rule out the scenarios above? Also could you please share the code of the HTTP request (200/201 etc)?

Hi Thakur,

If I debug the result right after the request, I get the data from the second ticket that was created in my response:

stdClass Object
(
[cc_emails] => Array
(
)

[fwd_emails] => Array
    (
    )

[reply_cc_emails] => Array
    (
    )

[ticket_cc_emails] => Array
    (
    )

[fr_escalated] => 
[spam] => 
[email_config_id] => 
[group_id] => 
[priority] => 1
[requester_id] => 80063016367
[responder_id] => 
[source] => 1
[company_id] => 
[status] => 2
[subject] => Test
[support_email] => 
[to_emails] => 
[product_id] => 
[id] => 128056
[type] => Vraag
[due_by] => 2022-09-09T12:58:12Z
[fr_due_by] => 2022-09-08T12:58:12Z
[is_escalated] => 

[created_at] => 2022-09-07T12:58:12Z
[updated_at] => 2022-09-07T12:58:12Z
[tags] => Array
    (
    )

[attachments] => Array
    (
    )

[nr_due_by] => 
[nr_escalated] => 

)

There is only one way to get to this request and I don’t see my request beeing fired twice in my console…

Hi Thakur,

I found the problem. It was a silly mistake in my code. Stupid I didn’t notice it.
The request is beeing executes twice, once in my if-statement and then the second time on the line after my if-statement.

Thanks for the support!

2 Likes

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.