If else condition in callback function of frappe.call

In a frappe call i want to run two frappe calls nde for set value and second for insert in a in-else statement but only one statement is running everytime. Any help ??

share your scriptā€¦

for (var i = 0; i < id_data2.length; i++) {

    var id_data3 = id_data2[i]
    frappe.call({
        method: "frappe.client.get_list",
        args: {
            doctype: "fup",
            fields: ['recid'],
            filters: {
                'recid': id_data3
            }

        },
        callback: function(r) {
            console.log(r.message)
            console.log('hi')
            if (typeof(r.message) == undefined) {
                frappe.call({
                    "method": "frappe.client.insert",
                    "args": {
                        "doc": {
                            "doctype": "fup",
                            "name": id_data3,
                            "followup_1": message_s,
                            "recid": id_data3
                        }
                    }

                });
            } else {
                frappe.call({
                    "method": "frappe.client.set_value",
                    "args": {
                        "doctype": "fup",
                        "name": id_data3,
                        "fieldname": {
                            "followup_2": message_s
                        },
                    }
                });
            }

        }

    });




}

Any help or suggestions

Return type of getList is list means array. so itā€™s not undefined. It should be blank array. You can use length for that.

for (var i = 0; i < id_data2.length; i++) {

    var id_data3 = id_data2[i]
    frappe.call({
        method: "frappe.client.get_list",
        args: {
            doctype: "fup",
            fields: ['recid'],
            filters: {
                'recid': id_data3
            }

        },
        callback: function(r) {
            console.log(r.message)
            console.log('hi')
            if (r.message.length>0) {
                frappe.call({
                    "method": "frappe.client.insert",
                    "args": {
                        "doc": {
                            "doctype": "fup",
                            "name": id_data3,
                            "followup_1": message_s,
                            "recid": id_data3
                        }
                    }

                });
            } else {
                frappe.call({
                    "method": "frappe.client.set_value",
                    "args": {
                        "doctype": "fup",
                        "name": id_data3,
                        "fieldname": {
                            "followup_2": message_s
                        },
                    }
                });
            }
        }
    });
}
1 Like