Can scheduled events be installed from onAppInstalled handler?

I can’t seem to get scheduled events working in production, while they do work when run in the FDK.

I schedule a reoccurring event in the handler for appInstalled as follows;

exports = {
  "events": [
    { event: "onAppInstall", callback: "onAppInstallCallback" },
    { event: "onAppUninstall", callback: "onAppUninstallCallback" },
    { event: 'onScheduledEvent', callback: 'scheduledEventHandler' },
  ],
  onAppInstallCallback: function() {
    const scheduleAt = new Date(new Date().valueOf()+1000*60*6).toISOString();
    $schedule.create({
      name: "poll_trigger",
      data: { },
      schedule_at: scheduleAt,
      repeat: {
        time_unit: "minutes",
        frequency: 5
      }
    }).then(function(data) {
      console.log("Installed polling schedule")
      renderData(null, data);
    }, function(err) {
      console.log("Unable to install polling schedule", err)
      renderData(err);
    });
  },
  onAppUninstallCallback: function() {
    $schedule.delete({ 
      name: "poll_trigger" 
    }).then(function(data) {
      console.log("Uninstalled polling schedule");
      renderData(null, data);
   }, function(err) {
      console.log("Unable to uninstall polling schedule", err);
      renderData(err);
    });
  },
  scheduledEventHandler: function(args) {
    console.log("Processing schedule event")
  }
} 

This works without problem when run in the FDK but when installed as a custom app in my instance. There is no log messages either from the installation methods nor from the scheduledEventHandler that would indicate the handler was called.

Is it supposed to be possible to register scheduled events from the onAppInstalled handler? How would I else have the schedule installed automatically as part of the installation by my user?

@kaa,
Welcome to the community!
The code snippet seems to be fine!
Can you please share the manifest file for our reference?
we just want to know if you are using platform version 2.0 or 2.1,
event definition is different from one version to another. if you are using 2.1 then you need to explicitly specify the event in manifest.

Hope it helps :slight_smile:
Thanks

1 Like

Here’s the manifest, it seems like 2.0

{
  "platform-version": "2.0",
  "product": {
    "freshservice": {}
  },
  "dependencies": {
    "base-64": "0.1.0",
    "js-sha256": "0.9.0",
    "jquery-deferred": "0.3.1"
  },
  "whitelisted-domains": [
    "https://*.freshservice.com"
  ]
}

Is there documentation about how to declare the event in the manifest file?

@kaa,
I think the platform version you are using seems to be fine!
Let me check and get back to you in a while :slight_smile:

Thanks

Thanks for the prompt reply!

When installed not from the onAppInstalled event but from onTicketUpdated the scheduling seems to work.

@kaa,

can you please try with the below snippet

exports = {
  events: [
    { event: "onAppInstall", callback: "onAppInstallCallback" },
    { event: "onAppUninstall", callback: "onAppUninstallCallback" },
    { event: "onScheduledEvent", callback: "scheduledEventHandler" },
  ],
  onAppInstallCallback: function() {
    const scheduleAt = new Date(new Date().valueOf()+1000*60*6).toISOString();
    $schedule.create({
      name: "poll_trigger",
      data: { },
      schedule_at: scheduleAt,
      repeat: {
        time_unit: "minutes",
        frequency: 5
      }
    }).then(function(data) {
      console.log("Installed polling schedule")
      renderData(null, data);
    }, function(err) {
      console.log("Unable to install polling schedule", err)
      renderData(err);
    });
  },
  onAppUninstallCallback: function() {
    $schedule.delete({ 
      name: "poll_trigger" 
    }).then(function(data) {
      console.log("Uninstalled polling schedule");
      renderData(null, data);
   }, function(err) {
      console.log("Unable to uninstall polling schedule", err);
      renderData(err);
    });
  },
  scheduledEventHandler: function(args) {
    console.log("Processing schedule event")
  }
} 

Just tested with my local environment, and it is working.
let me know if you are still facing the issue :slight_smile:

Thanks

Thanks, but I’m not sure what you mean. The code you posted is the exact same I had in my original question, the only change is the quotes around the events property which really doesn’t matter.

Please note that my original code too works in local development but not in production as mentioned in the first sentence of my question.

@kaa,
I just tried with the quotes in prod it is not worked, and after that, I tried removing quotes and it got worked.
I request you to try the same and let me know :slight_smile:
mean while I will investigate it in the local development (FDK)

Thanks

1 Like

No change for me without quotes unfortunately.

@kaa
Are you getting any console logs that you added in the snippet when you installed the app?

Nothing unfortunately, but when I run the same triggered by onTicketUpdate it’s fine.

Can you please share me the app zip which you are trying, I will try the same in my test environment,
along with that possible share me the app url (get it from dev portal)

1 Like