How to auto fetch value form field automaticly

i searched in the forum a lot to check how do this but i fail
my situation is
i have a doctype it’s name is : clients orders have these fields
1- client --> linked to Clients
2- order number --> data Field

and another doctype it’s name is : Clients Orders Monthly Invoice
i hava in this doctype a field its type is Table
linked to a table have these fields
1- Client --> linked to Clients
2- Client Orders —> i want this field to automatically Filter the results to only Sort the Chosen Client Orders ( not to Sort all Orders in the first doctype called clients orders )
3- Order Number --> and this field to automatically get the client Order number from previous field

this for example i made it simple as i can
i appreciate your time in reading and trying to help me

Hi @fatheyabdelslam,

if I understand you correctly you want to create a new record of “Clients Orders Monthly Invoice”, which has the link to the client. Then you want the script to fetch the client orders and populate your child table?

If this is the case, you could run a frappe.client.get_list and use the result to populate your child table. Something like

frappe.ui.form.on("Clients Orders Monthly Invoice", {
  client: function(frm) {
    // this function is called when the value of client is changed.
	
    // get all client orders of the current customer
    frappe.call({
		method: 'frappe.client.get_list',
		args: {
			doctype: 'Client Orders',
			filters: [
				['client', '=', frm.doc.client]
			],
			fields: ['name', 'order_number']
		},
		callback: function(response) {
			var orders = response.message;
			if (orders) {
				// loop through orders
				orders.forEach(function (order) {
					// add child table entries
					var child = cur_frm.add_child('client_orders');
					frappe.model.set_value(child.doctype, child.name, 'order_number', order.order_number);
					cur_frm.refresh_field('client_orders');
				});
			}
		}
	});
  }
})

You can extend the filters as required (I guess with a time filter as well). Maybe you also want to clear the child table before filling it again.

Hope this helps :wink:

2 Likes

this is more than i want i was intend to just filter the result and i’ll choose manually the orders
this is more advanced than my level
but thank you very much for this