How to force running jobs server-side
We frequently write quick-fix jobs, or even some quite heavy ones which are bound to execute client-side by default. The workaround is to create an Action MenuItem pointing at the job, and setting RunOn to be Server. Then next time we need to use the code we forget about that, and it takes awfully lot to complete execution. We need to force running jobs server-side in X++ somehow, for which I do have a neat solution.
We could check if the job is running on a client or on the AOS. Then we can call the menu function to force open it server-side from code.
// Place validation at the beginning of a job that is supposed to be server-bound
new MenuFunction(menuitemActionStr(YourJobActionMenuItem), MenuItemType::Action).run();
Here is a quick example showing it in action:
This will guarantee faster execution times for database calls and resource-intensive processes, granting permissions required to be server-bound, or code that relies on reflection such as traversing AOT. We do not need to worry about remembering whether it should run on the client or not, or do not have to find the correct menu item manually. It just works!