I have array initially declared in global scope as an empty array then inside a function callback. I have to push response message elements to this array.
But I need to get this update array outside callback of function
var array = []
frappe.call({
method: "frappe.client.get_list",
args: {
doctype: 'Attendance',
filters: [['attendance_date', '>=', values.from_date],
['attendance_date', '<=', values.to_date]
]
},
"callback": function (response) {
console.log(response.message)
for (var i in response.message) {
frappe.call({
method: "frappe.client.get_value",
args: {
doctype: 'Attendance',
fieldname: ['branch'],
filters: { 'name': ['=', response.message[i].name] }
},
"callback": function (data) {
frappe.call({
method: "frappe.client.get_value",
args: {
doctype: 'Medical Crew Salaries',
fieldname: ['total'],
filters: { 'cost_center': ['=',data.message.branch]}
},
"callback": function (r) {
array.push({ branch: data.message.branch, total: r.message.total })
}
})
}
})
}
}
})
console.log(array.length)
var result=[]
array.reduce(function(res, value) {
if (!res[value.branch]) {
res[value.branch] = { branch: data.message.branch, total: 0 };
result.push(res[value.branch])
}
res[value.branch].total += r.message.total;
return res;
}, {});
console.log(result)
frappe.model.with_doctype('Journal Entry', function() {
var journal_entry = frappe.model.get_new_doc('Journal Entry');
for(var c in result){
var journalentry_account= frappe.model.add_child(journal_entry, 'accounts');
journalentry_account.debit_in_account_currency=result[c].total
journalentry_account.cost_center=result[c].branch
}
frappe.set_route('Form', 'Journal Entry', journal_entry.name);
})
The reason why I need to do that is that I need to loop trough this array outside, the first for loop array.