Cur_frm.add_fetch but under event based on changed field


When selecting the field “assigned_driver”, I need to set values of multiple fields, but each field is link for different doctype. That is why I decided to use the event trigger based on changed field (the selected one), because I need to do more than one thing (multiple settings).

frappe.ui.form.on(‘Trip Order’, ‘assigned_driver’, function(frm) {
cur_frm.add_fetch(‘assigned_driver’, ‘employee_name’, ‘driver_name’)

When I am using cur_frm.add_fetch(‘assigned_driver’, ‘employee_name’, ‘driver_name’) out of the trigger, it is working fine and that is normally. What is the equivalent for it that let me set the value of the field driver_name under the trigger event?


OK, I think that I found the solution.
As I see, it is possible to use both:
It is possible to use add_fetch and to use the trigger event when selecting the field.
So I can set the values of other fields using the event and I can set the value of the field that is related to the link using the add_fetch.

But I have another question which is related to this post:
I have a field called vehicle which is link to vehicle, and in the vehicle doctype, there is driver field.
What is the best way to set the vehicle field to right vehicle when the user select the driver field?
In other words:
At vehicle doctype, there is a document name Vehicle1 and inside it the driver is Driver1, I need to assign the vehicle field at doctype trip order when selecting the driver.

The below images are help:


frappe.ui.form.on('Trip Order', 'assigned_driver', function(frm) {
    cur_frm.add_fetch(‘assigned_driver’, ‘employee_name’, ‘driver_name’);
    // var x = frappe.db.get_value('Vehicle', {'driver_name': cur_frm.doc.driver_name}, 'name'); // based on driver_name
    // var x = frappe.db.get_value('Vehicle', {'driver_link_fieldname': cur_frm.doc.assigned_driver}, 'name');
    cur_frm.set_value('vehicle', x);

One way I could think of is this. Maybe you get some idea from this.