OnTicketCreate handler is not triggering the methods inside its call back

Hi Team,

I am trying to update the ticket custom property field on product event onTicketCreate. I have two functions inside the OnTicketCreate callback in which one of the method which updates the entity db and other one which calls the ticket update API. But the entity update method is getting called but other one which calls the ticket update API is not triggering.

I have tried placing the ticket update API method inside the entity db success function, before calling the entity function and after calling the entity function. In all these scenarios my update ticket API function is not getting called.

Could you please help me with this. I have added the code below for your reference.

The updateTicketProperty method is not getting called on ticket create.

exports = {
    onTicketCreateHandler: function (args) {
        const { data: { ticket: { id, updated_at: updatedAt }, requester: { name } } } = args
        const ticketstatus = args["data"]["ticket"]["status"];
  
        if (ticketstatus === 3) {
            storeTicketDetailsToEntity(id, updatedAt)
        }
    }

}

function storeTicketDetailsToEntity(id, updatedAt) {

    const {
        actualStatusChangeTimestamp,
        nextTriggerTimestamp,
        nextTriggerDate,
        nextTriggerHour,
        nextTriggerMinute
    } = calculateFieldsToUpdate(updatedAt)


    const ticketDetails = {
        ticket_id: id,
        actual_status_change_timestamp: actualStatusChangeTimestamp,
        next_trigger_timestamp: nextTriggerTimestamp,
        next_trigger_date: nextTriggerDate,
        next_trigger_hour: nextTriggerHour,
        next_trigger_minute: nextTriggerMinute,
        overall_trigger_count: 1,
        current_trigger_count: 0,
        ticket_job_id: 0
    }

    // update custom field trigger available to true
    updateTicketProperty(ticketData['data']['ticket_id'], true)

    $entity.get('ticketDetails').create(ticketDetails).then(function (data) {
        console.info('Ticket data created successfully in DB', data)
        updateTicketProperty(ticketData['data']['ticket_id'], true)
    }, function (err) {
        console.error("Error occurred while creating ticket data in DB", err)
    })

     updateTicketProperty(ticketData['data']['ticket_id'], true)

}

function updateTicketProperty(ticketId, value) {
    console.info("In updateTicketProperty", ticketId, value)
    const domain = `https://domain.freshdesk.com`
    const url = `${domain}/api/v2/tickets/${ticketId}`
    const headers = {
        Authorization: `Basic ` + btoa('api key' + ":X"),
        'Content-Type': 'application/json'
    }
    const customFields = {
        cf_trigger_available: value
    }
    const body = JSON.stringify({ "custom_fields": { ...customFields } })

    const options = {
        headers: headers,
        body: body
    }
    $request.put(url, options).then(function (data) {
        console.info('Custom field updated successfully', data)
    }, function (err) {
        console.error('Error in updating the custom fields', err)
    })
}

Hi Team, Could you please provide an update on this.

Thank you

Hi team,

We have fixed this issue. It was due to the small code error while passing the arguments to the updateTicketProperty method. The argument should be data[‘record’][‘data’][‘ticket_id’] instead of data[‘data’][‘ticket_id’].

Thank you

1 Like