Cloud Function Cronjob

Hello, I want to create a cronjob that will run once a day and generate a number between -5 and -10 that will be within the User class and have the field name of percentage. How can I execute this using Moralis… Everyday the new number will replace the old number for all of the users, and each users number will be randomized so they don’t all get the same number,

Hi omni,

For a task that runs once per day you can create a Job and a Job Schedule

For info on interacting with Users and the database see

Yes, but this does very little to explain how I can access all the users and change each users specific field. Could you show me the code that could possibly do that?

Im very new to javascript so I need a little hand

Gratz for taking the journey to learn how to code! You now have a LOT of learning to do :slight_smile: Read the Moralis docs, do the tutorials on the Moralis Youtube channel, join the Ivan on Tech Academy.

To modify users you’ll need query the database. And because a logged in user only has permission to modify their own information, modifying other user information must be done on the Moralis Server in a Cloud Function as it requires using the Master Key.


Cloud Functions:

There is a github repo with a bunch of demo projects. One of them shows how to manage user profiles and the code there might be helpful.

User profile demo:

All demo apps:

1 Like

Yes but how do you access every user this is only telling me how to save data to a particular user, If you could send boilerplate code instead of vague links that would be nice.

I have encountered a similar problem, I need to change 2 columns for every user once a week. Boilerplate code explaining how this can be done would be a huge help for me.

I read through the links you sent but I cant seem to find anything that really explains what I need done, I don’t need you to write it all out but a little push would be nice

Hi @moe_mentum @omni

Paste this to the Cloud Functions:

Moralis.Cloud.job("randomGen", async(request) => {
  const query = new Moralis.Query("User");
  let users = await query.find({useMasterKey:true});
  for (user of users) {
    function getRandomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min)) + min; // does not include max, but includes min
    user.set("score", getRandomInt( -5, -1 ));
    await, {useMasterKey:true});

Then create a Job and set repeat settings:

I made just an example of what you want. Use my code, understand how it works and you can change it to fit your needs.

Hope it will help you :wink:
Happy BUIDLing :muscle:

1 Like

Thank you so much, this was a huge save


(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)