Grab a value from another doctype, which was in another doctype defined in a field

Dear All,

I want to take the country code value from the “primary_customer_address” field in DocType “Customer”. The process is that everytime i created a new customer, when clicking Save button, I want to be able to get the country code (IN, ID, US, etc) from the customer primary address and store it in a variable.

I know that the data flow is from Field “Code” in DocType “Country”, which is selected from the field “Country” in DocType “Address”, which is selected in field “customer_primary_address” in DocType “Customer”. I have no idea how get the value from the database that deep.

I’ve tried db.get_value, but it seems to not working at all.

frappe.ui.form.on('Customer', {
	validate(frm) {
	    if(frm.doc.customer_primary_address) {
            frappe.db.get_value("Address", {"name": frm.doc.customer_primary_address}, "country", function(value) {
                var countryName = value;
                console.log(countryName);
                
                var countryCode = frappe.db.get_value("Country", {"name": countryName}, "code")
                console.log(countrycode);
            });
	    }
	}
});

Any idea on how? Please help. Thank you

Please check the video: Fetch Child table form one doctype to other doctype - #6 by NCP

OK, I’ll try your method. But, while waiting, I tried another method like this:

  1. Create a custom field named ‘coi’ which linked to DocType Country
  2. create this function:
frappe.ui.form.on('Customer', {
	coi: function(frm) {
	    if(frm.doc.coi) {
            frappe.db.get_value('Country', frm.doc.coi, "code").then(res => {
                let x = res.message
                var countryCode = x.code;
	        });
                console.log(countryCode);
	    }
	}
});

But now the countryCode variable is undefined. Did i do wrong?

I also tried this, to no avail:

frappe.ui.form.on('Customer', {
	coi: function(frm) {
	    var countryCode = null;
        frappe.db.get_value('Country', frm.doc.coi, 'code').then(r => {
            this.countryCode = r.message.code;
        });
        
        console.log(countryCode);
    }
});

The thing is, if i just use console.log(r.message.code);, it works and returns the country code into console. But if I try to send it to another variable, it just doesn’t work. Even trying this:

var countryCode = frappe.db.get_value('Country', frm.doc.coi, 'code');

which is how the docs write about, is still not working. If I check the console, countryCode is just an object of Promise, which is definitely not how it is in the docs/tutorial: https://frappeframework.com/docs/user/en/api/database.

Any help will be appreciated. Thank you

Edit: this code

var countryCode;
frappe.db.get_value('Country', frm.doc.coi, 'code').then(result => {this.countryCode = result.message.code});
console.log(countryCode);

or

var countryCode;
frappe.db.get_value('Country', frm.doc.coi, 'code').then(result => {frm.countryCode = result.message.code});
console.log(countryCode);

or even

var countryCode;
frappe.db.get_value('Country', frm.doc.coi, 'code').then(result => {countryCode = result.message.code});
console.log(countryCode);

results are undefined.