I have two Doctypes named Log_Test and Log_Schedule which have two separate child tables each
Log_Test_Machines, Log_Test_Readings and Log_Schedule_Machines, Log_Schedule_Readings
I have created a template in Log_Test doctype in which i have put the data of different fields in both of its child tables
machine_group, unit_measure, unit, min_max
unit_measure, unit, min_max
min_max is a data field and all other are link fields i have put the data in the Log_Test_Machines and Log_Test_Readings
i have created a custom button in Log_Test Create Schedule which will create a new log_schedule with details of its child tables and its fields
Custom Button Code :-
frappe.ui.form.on(‘Log_Test’, {
refresh: function(frm) {
frm.add_custom_button(__(“Create Schedule”), function() {
frm.events.make_log_schedule(frm);
}, __(‘Create’));
},
make_log_schedule: function(frm) {
frappe.model.open_mapped_doc({
method: “workwings.plcm.doctype.log_schedule.make_log_schedule”,
frm: frm,
run_link_triggers: true
});
}
});
Code to create new Log_Schedule document :-
import json
import frappe
from frappe import _, msgprint
from frappe.model.mapper import get_mapped_doc
from frappe.query_builder.functions import Sum
from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, new_line_sep, nowdate
from frappe.model.document import Document
class Log_Test(Document):
@frappe.whitelist()
def make_log_schedule(source_name, target_doc=None):
“”“Creates a new Log_Schedule document based on a Log_Test document and its child tables.”“”
log_test = frappe.get_doc("Log_Test", source_name)
# Create the new Log_Schedule document
log_schedule = frappe.new_doc("Log_Schedule")
# Copy relevant fields from Log_Test to Log_Schedule
log_schedule.update({
"machine_related_readings": log_test.machine_related_readings,
"other_measurable_readings": log_test.other_measurable_readings,
})
# Create child table entries for Log_Schedule_Machines
for machine in log_test.get("log_test_machines"):
new_machine = frappe.new_doc("Log_Schedule_Machines")
new_machine.log_test = log_schedule.name
new_machine.machine_group = machine.machine_group
new_machine.unit_measure = machine.unit_measure
new_machine.unit = machine.unit
new_machine.min_max = machine.min_max
new_machine.save()
log_schedule.append("log_schedule_machines", new_machine)
# Create child table entries for Log_Schedule_Readings
for reading in log_test.get("log_test_readings"):
new_reading = frappe.new_doc("Log_Schedule_Readings")
new_reading.log_test = log_schedule.name
new_reading.unit_measure = reading.unit_measure
new_reading.unit = reading.unit
new_reading.min_max = reading.min_max
new_reading.save()
log_schedule.append("log_schedule_readings", new_reading)
# Save the new Log_Schedule document
log_schedule.save()
return log_schedule
I am getting this error when clicking create schedule button :-
App Versions
{
"frappe": "15.15.0",
"workwings": "0.0.1"
}
Route
Form/Log_Test/RIPL-PP-AFBC-2024-00046
Traceback
Traceback (most recent call last):
File "apps/frappe/frappe/app.py", line 110, in application
response = frappe.api.handle(request)
File "apps/frappe/frappe/api/__init__.py", line 49, in handle
data = endpoint(**arguments)
File "apps/frappe/frappe/api/v1.py", line 36, in handle_rpc_call
return frappe.handler.handle()
File "apps/frappe/frappe/handler.py", line 49, in handle
data = execute_cmd(cmd)
File "apps/frappe/frappe/handler.py", line 85, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File "apps/frappe/frappe/__init__.py", line 1682, in call
return fn(*args, **newargs)
File "apps/frappe/frappe/utils/typing_validations.py", line 31, in wrapper
return func(*args, **kwargs)
File "apps/frappe/frappe/model/mapper.py", line 23, in make_mapped_doc
method = frappe.get_attr(method)
File "apps/frappe/frappe/__init__.py", line 1672, in get_attr
return getattr(get_module(modulename), methodname)
AttributeError: module 'workwings.plcm.doctype.log_schedule' has no attribute 'make_log_schedule'
Request Data
{
"type": "POST",
"args": {
"method": "workwings.plcm.doctype.log_schedule.make_log_schedule",
"source_name": "RIPL-PP-AFBC-2024-00046",
"args": null,
"selected_children": "{}"
},
"freeze": true,
"freeze_message": "",
"headers": {},
"error_handlers": {},
"url": "/api/method/frappe.model.mapper.make_mapped_doc",
"request_id": null
}
Response Data
{
"exception": "AttributeError: module 'workwings.plcm.doctype.log_schedule' has no attribute 'make_log_schedule'",
"exc_type": "AttributeError"
}