OnTicketUpdateHandler not triggering for some tickets

Hello.

I have developed a new app. Everything is working perfectly when I am triggering it through the localhost even trigger, but when moved to production some tickets do not trigger the event and I am not getting why.

The idea of the App is to edit a ticket field [cf_temperature_escalation] (Dropdown) when another field is marked at certain value: [cf_temperature] == Hot
It does edit the value after some time (For testig propouses I just set 6 minutes). Also the scheduler should be cleared once the ticket is no longer on Hot.

So the code is the next one:

exports = {

    events: [{
            event: 'onTicketUpdate',
            callback: 'onTicketUpdateHandler'
        },
        {
            event: 'onScheduledEvent',
            callback: 'onScheduledEventHandler'
        },
    ],

    onScheduledEventHandler: function(args) {
        console.log("This should be after one minute");
        console.log("Data: " + args['data']['ticket_id']);
        var TicketID = args['data']['ticket_id'];
        var defconLevel = args['data']['defcon_level'];
        var scheduledDate = new Date();
        var url = 'https://[DOMAIN].freshdesk.com/api/v2/tickets/' + TicketID;

        switch (defconLevel) {
            case 'DEFCON 5':
                defconLevel = 'DEFCON 4';
                scheduledDate.setMinutes(scheduledDate.getMinutes() + 6);
                break;

            case 'DEFCON 4':
                defconLevel = 'DEFCON 3';
                scheduledDate.setMinutes(scheduledDate.getMinutes() + 6);
                break;

            case 'DEFCON 3':
                //console.info("Ticket : " + ticket_id + ". Triggering Defcon 3");
                defconLevel = 'DEFCON 2';
                scheduledDate.setMinutes(scheduledDate.getMinutes() + 6);
                break;

            case 'DEFCON 2':
                defconLevel = 'DEFCON 1';
                scheduledDate.setMinutes(scheduledDate.getMinutes() + 6);
                var skip = true;
                break;

            case 'DEFCON 1':
                defconLevel = 'DEFCON 1';
                scheduledDate.setMinutes(scheduledDate.getMinutes() + 6);
                var skip = true;
                break;

            default:
                defconLevel = "DEFCON 3";
                scheduledDate.setMinutes(scheduledDate.getMinutes() + 6);
        }


        if (!skip) {
            var options = {
                //'method': 'PUT',
                'headers': {
                    'Content-Type': 'application/json',
                    'Authorization': 'Basic API_KEY'
                },
                body: JSON.stringify({
                    "custom_fields": {
                        "cf_temperature_escalation": defconLevel
                    }
                })
            };

            $request.put(url, options).then(function(data) {
                console.log("Ticket: " + TicketID + " has been updated to Defcon Level: " + defconLevel);
                console.log(data);
                $schedule.create({
                    name: TicketID,
                    data: {
                        ticket_id: TicketID,
                        defcon_level: defconLevel
                    },
                    schedule_at: scheduledDate.toISOString()
                }).then(function() {
                    console.log("Scheduler set for ticket: " + TicketID + " At: " + scheduledDate.toISOString());
                }, function(error) {
                    console.error(error);
                });


            }, function(error) {
                console.log("Something went wrong when trying to update from ticket: " + TicketID);
                console.log(error);
            });
        } else {
            console.log("Ticket: " + TicketID + " has been updated to Defcon Level: " + defconLevel);
        }
    },

    // args is a JSON block containing the payload information.
    // args['iparam'] will contain the installation parameter values.
    onTicketUpdateHandler: function(args) {
        // var group = args['data'].['ticket'].['group_id']==80000439706;

        // New handler
        var TicketID = args['data']['ticket']['id'];
        console.log("TEST");
        //console.log(args['data']['ticket']['custom_fields']);
        console.log("New update." + TicketID + " Temperature: " + args['data']['ticket']['custom_fields']['cf_temperature_1722398']);

        //if (args['data']['ticket']['custom_fields']['cf_hot_escalation_scheduler__1722398']){
        if (args['data']['ticket']['custom_fields']['cf_temperature_1722398'] === 'Hot') {
            var defconLevel = args['data']['ticket']['custom_fields']['cf_temperature_escalation_1722398']
            var scheduledDate = new Date();
            console.log(scheduledDate);
            scheduledDate.setMinutes(scheduledDate.getMinutes() + 6);
            console.log(scheduledDate);

            $schedule.create({
                name: TicketID.toString(),
                data: {
                    ticket_id: TicketID.toString(),
                    defcon_level: defconLevel
                },
                schedule_at: scheduledDate.toISOString()
            }).then(function() {
                console.log("Scheduler set");
            }, function(error) {
                console.log(error);
            });
            //console.info("This should be before");

        } else {
            $schedule.delete({
                name: TicketID.toString()
            }).then(function() {
                console.log("Scheduler deleted");
            }, function(error) {
                console.log(error)
            });
        }

    }
};

Proof that I am updating the ticket:

But nothing is reported into the logs:

There should be at least the “TEST” message, as far as nothing is there.

Fun fact, it works when doing via the localhost:

Any clue about what might be going on?
It looks like it is not even working at all

1 Like

Is the temperature a custom field? If so, if a custom field only is changed, onTicketUpdate events won’t be triggered.

3 Likes

Rly? I did not knew that.
Thanks for the information. Is there any callback that I can trigger when a custom field is changed?

2 Likes

In case somebody needs it.
I solved it creating a new group.
When the custom field is edited to the value that I want, I create a ticket for that handler. I do aslo store the ID of the parent on a Custom Field (I do thata to prevent the Parent-Child relation and it implications, so everything is transparent to the user)
Then when a ticket is created for that particular group I do create the schedulers.

When the parent ticket is closed, resolved or the condition is no longer valid I deleted the ticket on the handler group so I can catch that event on the API and remove the handler.

It is an ugly workaround, but it works.

3 Likes

Hey @Chavas just for clarity, there is a way to enable the custom field updates. Do take a look at the thread -> Questions on the onTicketUpdate trigger - App Development - Freshworks Developer Community

2 Likes