While clicking a particular ID on the list view it opens the corresponding value if the form is not saved I need to save that form. I tried frm.doc.__unsaved but for the first log shows undefined if I go back a second time and click the ID it shows a single view and logs show 1 and save the form
To properly check whether the form is unsaved, you should consider using the frm.is_dirty() method, which is a reliable way to check if there are any unsaved changes on the form
refresh(frm) {
console.log(frm.is_dirty()); // Log whether the form is dirty (unsaved)
if (frm.is_dirty()) {
frm.save(); // Save the form if there are unsaved changes
}
}
def on_submit(doc, method):
# Check if the drug_prescription table has any rows
if not doc.drug_prescription:
# Do nothing if no rows are present in the drug_prescription table
return
# Check if a Pharmacy record already exists for this Patient Encounter
existing_pharmacy = frappe.get_all(
"Pharmacy Prescription",
filters={"patient_encounter": doc.name},
limit=1
)
if not existing_pharmacy:
# Create a new Pharmacy record
pharmacy_doc = frappe.get_doc({
"doctype": "Pharmacy Prescription",
"patient_encounter": doc.name, # Link to Patient Encounter
"patient": doc.patient, # Map the patient field
"patient_name": doc.patient_name,
"gender": doc.patient_sex,
"age": doc.patient_age, # Map age
"encounter_date": doc.encounter_date, # Map encounter date
"encounter_time": doc.encounter_time, # Map encounter time
"healthcare_practitioner": doc.practitioner, # Map healthcare practitioner
"practitioner_name": doc.practitioner_name, # Map practitioner name
"department": doc.medical_department,
"status":"Booked"
})
# Append drug prescription details
for drug in doc.drug_prescription:
pharmacy_doc.append("drug_prescription", {
"medication": drug.medication, # Map medication
"drug_code": drug.drug_code, # Map drug code
"dosage": drug.dosage, # Map dosage
"period": drug.period, # Map period
"dosage_form": drug.dosage_form,
"medication_status":"Booked"
})
# Save the Pharmacy record
pharmacy_doc.insert(ignore_permissions=True)
frappe.db.commit()
this is the way I insert data in my prescription doctype, Now I’m accessing the listview of the prescription doctype and creating a new entry on the prescription it initially when I view it shows not saved
if doc.drug_prescription:
for drug in doc.drug_prescription:
Final Optimized Code
# Check if a Pharmacy record already exists for this Patient Encounter
existing_pharmacy = frappe.get_all(
"Pharmacy Prescription",
filters={"patient_encounter": doc.name},
limit=1
)
if not existing_pharmacy:
# Create a new Pharmacy record
pharmacy_doc = frappe.get_doc({
"doctype": "Pharmacy Prescription",
"patient_encounter": doc.name, # Link to Patient Encounter
"patient": doc.patient, # Map the patient field
"patient_name": doc.patient_name,
"gender": doc.patient_sex,
"age": doc.patient_age, # Map age
"encounter_date": doc.encounter_date, # Map encounter date
"encounter_time": doc.encounter_time, # Map encounter time
"healthcare_practitioner": doc.practitioner, # Map healthcare practitioner
"practitioner_name": doc.practitioner_name, # Map practitioner name
"department": doc.medical_department,
"status": "Booked"
})
# Append drug prescription details only if available
if doc.drug_prescription:
for drug in doc.drug_prescription:
pharmacy_doc.append("drug_prescription", {
"medication": drug.medication, # Map medication
"drug_code": drug.drug_code, # Map drug code
"dosage": drug.dosage, # Map dosage
"period": drug.period, # Map period
"dosage_form": drug.dosage_form,
"medication_status": "Booked"
})
# Save the Pharmacy record
pharmacy_doc.insert(ignore_permissions=True)