How to get all records of a doctype in Client Script

How can I get all documents of a Doctype in the client script?
I tried to use frappe.db.get_list but it does not work .

frappe.ui.form.on('Sale Order',  {
    setup(frm) {
        
        // get all added cost 
        var added_cost = frappe.db.get_list('Added Cost')
        $.each(added_cost , function(index , item){
              let row1 = frm.add_child('sale_order_added_cost', {
                added_cost:"Departure cost"
             });
            frm.refresh_field('sale_order_added_cost');
        })
       
    }
})

Instead of frappe.db.get_list try using only frappe.get
_list …
maybe this works …

@Sakshi_Choudhary
frappe.get_list did not work too.

when I run code below :

  var added_cost = frappe.db.get_list('Added Cost')
        console.log(added_cost)

I get this on my console :
image

but I can not access its data
how can i work this array which is showing in console?

for accessing the data from get_list , you have to iterate over it

@Sakshi_Choudhary

How can I do that?
please correct my code if it’s possible

Thanks in advance

The javascript version of get_list returns a Promise, which means that you cannot immediately use the added_cost variable in the way that you’re doing. You need to use some kind of promise handler. Something like…

frappe.db.get_list('Added Cost').then( function(added_costs) {
   $.each(added_costs , function(index , item){
       let row1 = frm.add_child('sale_order_added_cost', {
          added_cost:"Departure cost"
       });
       frm.refresh_field('sale_order_added_cost');
   })
});
1 Like