How to subtract custom fields from different doctypes

Good day guys.
I have Doctype A with custom field A and Doctype B with custom field B. I want to subtract custom field B from Custom field A. I also the custom field A refresh and show the remaining balance.
Thank you guys

Add a client script:

    frappe.ui.form.on("Doctype A",{
        field_a: function(frm){ 
               field_c = field_a - field_b;
               refresh_field("field_c");
        },
        field_b: function(frm){ 
               field_c = field_a - field_b;
               refresh_field("field_c");
        }

    });

field_b is on another doctype “Doctype B”. how can i sort it out

Fetch the field from Doctype B with a link field and use that instead of field_b