Creating Document using Script

Hi there,
I am trying to write a script for making a leave allocation document and filling the values in it through the script and also submitting it.
Is there a command for the same?
The thing is i want this leave allocation document to created for all the employees at the end of the month automatically.
For e.g. There is an employee say A so A is entitled 21 leaves per year i.e. 1.75 per month. So if A don’t take 1.75 leave in the month it is carry forwarded to next month. so in the next month the total is 3.5. So i am writing a script so that after the end of the month leave allocation document must be created for A for the next month and also gets submitted at the same time after filling in the values too.

@Ujjawal

You will have to write a hook in hooks.py for month based execution which will be executed at the start of the month. This all will be done in Python File for Leave Allocation.

Your hooks function should have functionality as follows,

  1. Get Employee Data - frappe.get_all('Employee')
  2. Get Leave Allocation data for past month - frappe.get_doc('Leave Allocation', {/Your Filters to fetch leave allocation for employee for the past month/}
  3. Create New Leave Allocation using carry forward data from the past month - doc = frappe.new_doc('Leave Allocation')
  4. doc.update({/all relevant data values along with your carry forward leaves/}

Hope this helps to solve your problem.

Hi @shreyasp,
I have written a script for the same the problem is i just dont know how i can create a new doc and add data to the newly created document and then later submit it.

I ll paste my code provide with your valuable feedback.

The Code below is whitelisted.
In hooks.py under function scheduler events can I mention this function for monthly basis?

As per the best practices for development never use frappe.db.sql as you are directly executing the queries on the database. A better way would be to execute frappe.get_doc and then specify the fields that you would like to operate on or frappe.db.get_value if you want to fetch one or two fields for the doctype.

After line 149

leave_allocation_doc = frappe.new_doc("Leave Allocation")
leave_allocation_doc.update({field1: value1, field2: value2, ..})
leave_allocation.save(ignore_permissions=True)
frappe.db.commit()

This should help you to create new leave allocation for the employee. Hope this helps to solve your problem.

1 Like

Can u throw some light on hooks.py as well.
I mean How can i schedule this event on monthly basis?

@Ujjawal

In ERPNext hooks.py add an event in scheduler_events

{
...
"monthly": ["erpnext.path.to.your.leave.allocation.function"]
...
}

This will add scheduler event that gets triggered First day of every month.

how can i set it some other day of every month?

@Ujjawal

At present Frappé hooks don’t allow such functionality, so in case you want to do trigger on specific day or month then you might have to update scheduler.py and all relevant files where scheduling is used or defined.

Thanks @shreyasp for your help

how can i retrieve value of check box? I mean how can check the checkbox from script

Just fetch values using frappe.db.get_value("Your Doctype", "Docname", "checkbox_field_name")

I mean how can i check the checkbox from script?

You can do that simply frappe.db.set_value("Your Doctype", "Docname", "checkbox_field_name", 1). This should do the trick for you.

Thanks for the help.
You guys are great!

@shreyasp
i am facing some trouble and am not able to understand what might be the problem.
i am getting an error unconverted data remains in upload attendance module.
I dont know what that means.

@Ujjawal

Let’s open up a new thread, since this error is related to data import tool. I am closing this thread.