DB Count returns garbage

Hi,
When I call

    frappe.db.count('Serial No', { item_code: '3921-024' })
    .then(count => {
        console.log("func count " + count);
        return count;
    })

It returns something like this

image

Why it’s not working? Real result should be 11.

ERPNext: v12.29.0 (version-12)

Frappe Framework: v12.26.0 (version-12)

Anyone?

Use this

frappe.db.count('Serial No', filters={"item_code": '3921-024' })
.then(count => {
    console.log("func count " + count);
    return count;
})

Then I get

form.js:557

       ReferenceError: filters is not defined
    at countDuplicates (eval at setup (script_manager.js:160:5), <anonymous>:75:41)
    at before_save (eval at setup (script_manager.js:160:5), <anonymous>:60:14)
    at a (script_manager.js:90:16)
    at script_manager.js:108:22

Something makes no sense here.

image

    before_save: function(frm) {
        var val = "";
        var cnt = 0;
        
        for (const target of uniqueFields)
	    {
	        field = frm.get_field(target[0]);
	        val = field.get_value();
	        if (val !== '' && typeof val !== 'undefined' && target[1])
	        {
	            cnt = countDuplicates(target[0], val);
	            target[1] = 0;
	            console.log("found duplicates: " + cnt);
	           /* if (cnt !== 0)
	            {
	                frappe.throw(__("Serial No with same " + target + " value already exists!"));
	                break;
	            }*/
	        }
	    }
    }


function countDuplicates(t, v)
{
    console.log("count func\ntarget " + t + "\nvalue " + v);
    frappe.db.count('Serial No', { item_code: '3921-024' })
    .then(count => {
        console.log('return ' + t + ' ' + count);
        return count;
    });
}

I would expect output in console something like this

count func
target sigfox_id
value ***
VM10431:96 return sigfox_id 1483
VM10431:80 found duplicates: undefined

VM10431:93 count func
target sigfox_pac
value ****
VM10431:96 return sigfox_pac 1483
VM10431:80 found duplicates: undefined

No matter what filter I put, it returns total number of docs in Serial No doctype.

Try this

frappe.db.count('Serial No', { filters: { item_code: '3921-024' }})

Count implementation, you can typically find any function’s implementation if you type frappe.db.count and click on returned func object.

6 Likes