Try ERPNext Buy Support Partners Foundation

Unable to filter the record I want (frappe.client.get_value)

Hi, I was trying to retrieve records from another doctype by the record name. Im not sure whether i have any syntax error here.

what i did here is i want to get location_name and group from my Fixed Asset doctype by filtering the record name, i had comfirm that the field name is correct and i suspect that i code wrongly at the filter part from frappe.call method. i had also check that targetAsset do able to store the value of frm.doc.fixed_asset using window alert

but i get undefined from my r.message, i use alert(r.message.name) to check at the callback function

frappe.ui.form.on("Asset Audit", "check", function(frm) {
var targetAsset = frm.doc.fixed_asset;
var targetLocation = frm.doc.location;
var targetGroup = frm.doc.group;
var totalMatch = frm.doc.total_matching;
var totalNoMatch = frm.doc.total_no_match;
frappe.call({
		method: 'frappe.client.get_value',
		args: {
			'doctype': 'Fixed Asset',
			'filters': { 'name': targetAsset},
			'fieldname': [
				'location_name',
				'group'
			]
		},
		callback: function (r) {
			alert(r.message.name);
			if (r.message.location == targetLocation && r.message.group == targetGroup){
				alert("Match!");
				totalMatch++;
				frappe.model.set_value(frm.doctype, frm.docname, "total_matching",totalMatch);
			}else{
				alert("No Match!");
				totalNoMatch++;
				frappe.model.set_value(frm.doctype, frm.docname, "total_no_match",totalNoMatch);
			}	
			
		}
    });	

});

you help is much appreciated

Hi @KAH_MENG_Long

with get_value you can callback only one field, not 2 like you want.
grafik

use instead get_list:

Thanks @joelios

Did you mean if i use get_list i can get multiple records?
for example if i filter by location_name and group_name at get_list,
in my target doctype i have 3 records by this filter,
then my “r” from callback function ( r ) will contains 3 records

so if i use $.each to loop it i can go through every record that i want?

@KAH_MENG_Long
Correct. If you have 3 records, you will get an array with r.message[0] (=1. Record), r.message[1] (=2. Record) and r.message[2] (=3.record) :slight_smile:

And i prefer to loop like:
for (i=0; i < r.message.lenght; i++) {
r.message[i]…
}

:wink:

Lets say i want to validate my booking time whether my fixed asset is available to book or not,
I have to go through all the records of my Asset Booking doctype to validate the date and I code like this,
but it does not work, it just let me save the record with any input of datetime.

frappe.ui.form.on("Asset Booking", "validate", function(frm) {
	var targetAsset = frm.doc.fixed_asset;
	 frappe.call({
            method: 'frappe.client.get_list',
            args: {
                'doctype': 'Asset Booking',
                'filters': { 'fixed_asset': targetAsset },
                'fieldname': [
                    'start_datetime',
		            'end_datetime'
                ]
            },
            callback: function(r) {
				for (i=0; i < r.message.lenght; i++) {
					if (frm.doc.start_datetime >= r.message[i].start_datetime && frm.doc.start_datetime <= r.message[i].end_datetime){	
					frappe.msgprint("The asset had been booked at the moment, please choose other date.");
					frappe.validated = false;
					}
					if (frm.doc.end_datetime <= frm.doc.start_datetime){	
						frappe.msgprint("Wrong enddatetime.");
						frappe.validated = false;
					}
					if (frm.doc.end_datetime >=r.message[i].start_datetime && frm.doc.end_datetime <= r.message[i].end_datetime) {
						frappe.msgprint("Wrong enddatetime which is in between people's booking.");
						frappe.validated = false;
					}
				}
			}
        });
})

find solution ?

For me the Solution is to change ‘feildname’ to ‘fields’ and working fine and of course
its length not lenght