Client Script To Auto Generate Lottery Number

Hello, please advise. I want to create lottery numbers with the following rules:
Customers will get 3 random lottery numbers every time their total purchase reaches 100,000.

Here is a client script that I tried to make, but it doesn’t seem to work.
Please provide suggestions and input so that this script can be used

frappe.ui.form.on('Sales Invoice', {
    after_submit: function(frm) {
        // Check if the total invoice amount is greater than or equal to 100,000
        if (frm.doc.grand_total >= 100000) {
            // Calculate the number of lottery numbers based on the invoice amount
            var numberOfLotteryNumbers = Math.floor(frm.doc.grand_total / 100000) * 3;
            // Generate lottery numbers
            var lotteryNumbers = [];
            for (var i = 0; i < numberOfLotteryNumbers; i++) {
                lotteryNumbers.push(Math.floor(Math.random() * 10000) + 1);
            }
            // Update the lottery numbers field in the document
            frappe.model.set_value(frm.doctype, frm.docname, 'lottery_number', lotteryNumbers.join(', '));
            frm.save();  // Save the document to store the generated lottery numbers
            frappe.msgprint('Lottery numbers generated: ' + lotteryNumbers.join(', '));
        }
    }
});

Did you set up the lottery field to be “Allow after submit”? You can change the trigger to after_save and see what happens.

Instead, you can use frappe.call to run a server-side method to update the submitted document.

Hi. Thank you for answering my question.

Yes, “allow on submit” has been checked for the lottery_number field.
The trigger has also been changed to after_save, but it still doesn’t seem to work.

Regarding the server script, I have never tried it before… do you have any suggestions?

Thank You

It’s hard to come with something if you don’t have all the info. I wouldn’t use the UI to do this though, but rahter a serverside approach. This ensures the integrity of the numbers created as well as their uniqueness. The trigger should still be after_save.

i tried to add code on sales_invoice.py

import random
from frappe.model.document import Document


class SalesInvoice(Document):
        def on_submit(self):
                        if self.customer_group in ["Langganan Gorontalo", "Grosir Gorontalo"] and self.base_grand_total >= 100000:
                                custom_nomor_undian = [random.randint(100000, 999999) for _ in range(3)]
                                lottery_numbers_str = ', '.join(str(n) for n in custom_nomor_undian)
                                self.custom_nomor_undian = lottery_numbers_str
                                self.ignore_validate_update_after_submit = True
                                self.save()

No error, but still no value change on my field

Could you try replacing it with this?

frm.doc.lottery_number = lotteryNumbers.join(,);

@Gembira_IT_Tech

import random
from frappe.model.document import Document


class SalesInvoice(Document):
        def on_submit(self):
                if self.customer_group in ["Langganan Gorontalo", "Grosir Gorontalo"] and self.base_grand_total >= 100000:
                            custom_nomor_undian = [random.randint(100000, 999999) for _ in range(3)]
                            lottery_numbers_str = ', '.join(str(n) for n in custom_nomor_undian)
                            self.db_set('custom_nomor_undian', lottery_numbers_str)

Its works! Absolutely perfectly.

Thank you very much

1 Like

@Gembira_IT_Tech

Just a small input, if you using doc_events hooks that it is fine but if you are modifying inside core sales_invoice.py folder that i would not suggest to do that.

Use doc_events hooks always to write server scripts. How to Write Server Script