Frappe: frm.doc.__unsaved Undefined on First Click in List View

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

refresh(frm) {
console.log(frm.doc.__unsaved)
if(frm.doc.__unsaved){
frm.save()

}}

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
    }
}

console.log(frm.dirty()) shows false only

Kindly use

console.log(frm.is_dirty());

instead of

console.log(frm.dirty())

Try using this code inside the refresh function:

refresh(frm) {
    console.log(frm.doc.__unsaved, frm.doc.__islocal);

    if (frm.doc.__islocal || frm.doc.__unsaved) {
        frm.save();
    }
}

console.log(frm.doc.__unsaved, frm.doc.__islocal);
Both are undefined only

I’m using this only console.log(frm.is_dirty()) is also undefined

try this

if (!frm.doc.__islocal) {
        frm.save();
    }

or only

refresh(frm) {
    console.log(frm.doc.__unsaved, frm.doc.__islocal);

    if (frm.doc.__islocal) {
        frm.save();
    }
}

no need to include : frm.doc.__unsaved

if(!frm.doc.__islocal){
frm.save()
}

Still I’m facing this issue

Do you mean that when you click the ID (e.g., PHA-PRES-00002), it shows ‘Not Saved’ even though you had already saved it before?

yes, in Listview I click the ID PHA-PRES-00002, and it redirects to single view right, in that it shows status as not saved like above image

Have you used frm.set_value in your JavaScript code without saving the form afterward?

can we see your full js file code

refresh(frm){
console.log(frm.is_dirty())
if(frm.is_dirty()){
frm.save()
}
}

is this a client script?

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

yes this is my client script

Add a check:

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)