Hi Community !!
I have created one doctype called “CSP Progress Report”. In that doctype, i have added one html field called “csp_report”. I have Task doctype, In that doctype , i have added one childtable called “Action Items” with fieldname “custom_action_item”. Now, i want the list of task data and their childtable data should be shown as a table in “CSP Progress Report” doctype html field “csp_report”. for eg: In task doctype, there are 3 tasks , each tasks has 2 rows in childtable. so it should show like 1st row show task 1 with ChildT row 1 and 2nd row childT row 2 but row 2 dont repeat the task 1 details. next 3rd row task 2 details with their ct row 1, etcc… . I used server script for call the datas from Task doctype.because there is permission issue when i use only client script.
frappe.ui.form.on('CSP Progress Report', {
onload(frm) {
frappe.call({
method: 'csprr',
callback: function(response) {
// Set the generated HTML content in the 'csp_report' field
frm.set_df_property('csp_report', 'options', response.message);
frm.refresh_field('csp_report');
}
});
}
});
def generate_csp_report():
# Fetch all Task records with necessary fields
tasks = frappe.db.get_list('Task', fields=[
'name', 'subject', 'custom_country', 'custom_region',
'custom_type_of_period', 'custom_month', 'custom_week'
])
# Initialize the HTML table
html = """
<style>
table {
width: 100%;
border-collapse: collapse;
font-size: 10px;
border: 1px solid black;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid black;
}
th {
background-color: #003366;
color: white;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #f1f1f1;
}
td[colspan] {
background-color: #f9f9f9;
}
</style>
<table>
<thead>
<tr>
<th>Task ID</th>
<th>Subject</th>
<th>Country</th>
<th>Region</th>
<th>Quarter</th>
<th>Month</th>
<th>Week</th>
<th>Action Item</th>
<th>Urgency</th>
<th>Due Date</th>
<th>Progress Status</th>
<th>Remarks</th>
<th>Status</th>
<th>Responsible Person</th>
<th>Action Items</th>
</tr>
</thead>
<tbody>
"""
# Loop through each Task record
for task in tasks:
# Fetch associated Action Items for the current Task
action_items = frappe.db.get_list('Action Items', filters={'task': task['name']}, fields=[
'action_item', 'urgency', 'due_date', 'progress_status', 'remarksif_any',
'status', 'responsible_person', 'action_items'
])
# For each action item, build the corresponding HTML rows
for idx, item in enumerate(action_items):
# Only include task details in the first row for each task
if idx == 0:
html += f"""
<tr>
<td>{task['name']}</td>
<td>{task['subject']}</td>
<td>{task['custom_country']}</td>
<td>{task['custom_region']}</td>
<td>{task['custom_type_of_period']}</td>
<td>{task['custom_month']}</td>
<td>{task['custom_week']}</td>
</tr>
"""
# Add the Action Item details to the table (without repeating Task details)
responsible_person_name = ''
if item.get('responsible_person'):
user = frappe.get_doc('User', item['responsible_person'])
responsible_person_name = user.full_name if user else ''
html += f"""
<tr>
<td></td> <!-- Empty cell for Task ID -->
<td></td> <!-- Empty cell for Subject -->
<td></td> <!-- Empty cell for Country -->
<td></td> <!-- Empty cell for Region -->
<td></td> <!-- Empty cell for Quarter -->
<td></td> <!-- Empty cell for Month -->
<td></td> <!-- Empty cell for Week -->
<td>{item.get('action_item', '')}</td>
<td>{item.get('urgency', '')}</td>
<td>{item.get('due_date', '')}</td>
<td>{item.get('progress_status', '')}</td>
<td>{item.get('remarksif_any', '')}</td>
<td>{item.get('status', '')}</td>
<td>{responsible_person_name}</td>
<td>{item.get('action_items', '')}</td>
</tr>
"""
# Close the table HTML
html += "</tbody></table>"
# Return the generated HTML to be used in the form field
return html