Invoke $schedule.create(…) during app installation or from serverside

I want to sync contacts from a third-party system to Freshdesk using a serverless app. I’ve implemented a scheduler that runs every 6 minutes to fetch data from the external system. If a contact does not exist in Freshdesk, it will be created via API; otherwise, it will be updated.

Local testing works fine using the onScheduledEvent simulation. However, after deploying the app to production, the scheduler does not trigger as expected.

I need to create the schedule during app installation from the server side. I’m using only a server.js file and Freshdesk platform 2.3. I attempted to create the schedule within the onAppInstall handler, but it failed.

How can I trigger the schedule creation correctly during app installation in a serverless app?

Hello Mannazhi,

Could you share more your implementation on how you have implemented in server.js

onScheduledEventHandler: async function(args) {
console.log(“Syncing Contacts!”);
//sync logic
},
onAppInstallEventHandler : async function(args) {
console.log('App installed with params '+ JSON.stringify(args));
const scheduleName = “sync_contact”;
try {

  try {
    let data = await $schedule.delete({
      name: scheduleName
    });
  }
  catch (error) {
    console.log("Error deleting schedule "+error);
  }

  const scheduleAt = new Date(Date.now() + 60 * 1000).toISOString(); // 6 min from now
  await $schedule.create({
    name: scheduleName,
    data: {},
    schedule_at: scheduleAt,
    repeat: {
      time_unit: "minutes",
      frequency: 6
    }
  });
  console.log("Schedule created.");

} catch (error) {
console.error(“createSchedule Error:”, error.message);
}
}

Hello @Arya_Mannazhi

Thanks for sharing the code snippet.

I see that on the callback function definition onAppInstallationEventHandler you are missing the renderData()

Kindly add that at the end and give it a try. I hope it helps.

Hi Mohammed,

It worked perfectly. Thank you very much for your support.

1 Like