How do I call a server function from a Freshservice event

I have a scheduled event that is supposed to call another server function. I am getting that the function is not defined. At the moment I am thinking that the task that is executed during the scheduled event is in a different scope of the server.js file. I can think of a solution to just add the code from the function that is being called by the scheduled event task, but this is unwanted redundancy. Is there another way I can access this function?

is it possible to invoke a server function from the server?

Example:

 onScheduledEventHandler: function(payload) {
    console.log("Logging arguments from onScheduledEvent: " +  JSON.stringify(payload));
    if("name" in payload.data) {
        if(payload.data.name === "sync") {
            console.log('starting the scheduled sync')
            try {
                somefunction(payload); //undefined error
            } catch (e) {
                console.log(e);
            }
        }
    }
    renderData();
  },
somefunction: function(args) {
//task to be performed
renderData();
}

Edit:
found a new app example in the docs that uses the following.

let obj = this;
obj.somefunction()

This was the solution for anyone that encounters a similar issue

1 Like

Hi @osanchez,

I hope, you have found the solution. Just using this in your code snippet as follows will work. It is the normal behavior of JavaScript.

this.somefunction(payload);