Fetch phone number script no longer working (since v8?)

Hi all,

I’ve been using a custom script to fetch the Customer Billing Address “phone” value and populate it onto the Sales Order Form into a custom field. This has worked until recently - I am assuming it stopped working after the V8 upgrade (could be wrong but may be a clue as to what changed).

Anyway can someone check my script and see where the error lies? Thanks:

 cur_frm.cscript.validate = function(doc, cdt, cdn){
   if (!cur_frm.doc.phone_number){
     frappe.call({
       'method': 'frappe.client.get_value',
       'async': false,
       'args': {
         'doctype': 'Address',
         'filters': {
           'customer': doc.customer,
           'address_type': 'Billing'
         },
         'fieldname': 'phone'
       },
       'callback': function(res){
         if (res.message && res.message.phone)frappe.model.set_value(cdt, cdn, 'phone_number', res.message.phone);
       }
     });
   }

@monojoker,

all the references are moved to the Dynamic Link child table for Address and Contact doctype. So while filtering you will not get the customer in the Address

Try the following filters

filters: [
 	["Dynamic Link", "link_doctype", "=", "Customer"],
 	["Dynamic Link", "link_name", "=", doc.customer],
 	["address_type", "=", "Billing"]
 ]
1 Like

@makarand_b Edit: OK I’ve fixed the script so it stopped erroring but I still don’t get the phone number populating. Have included screenshots to show the setup. Script is:

cur_frm.cscript.validate = function (doc, cdt, cdn) {
  if (!cur_frm.doc.phone_number) {
    frappe.call({
      method: 'frappe.client.get_value',
      async: false,
      args: {
        doctype: 'Address',
        filters: [
          ['Dynamic Link', 'link_doctype', '=', 'Customer'],
          ['Dynamic Link', 'link_name', '=', doc.customer],
          ['address_type', '=', 'Billing']
        ],
        fieldname: 'phone'
      },
      callback: function (res) {
        if (res.message && res.message.phone) {
          frappe.model.set_value(cdt, cdn, 'phone_number', res.message.phone);
        }
      }
    });
  }
};

@monojoker,

frappe.model.set_value(cdt, cdn, fieldname, value) is use to set the value in child table field and you are tring to set the value on Form,

Use frm.set_value('fieldname', value)

thanks @makarand_b I’ve tried both cur_frm.set_value and frm_set_value and both don’t work:

frm.set_value(cdt, cdn, 'phone_number', res.message.phone);

cur_frm.set_value(cdt, cdn, 'phone_number', res.message.phone);

Not sure what else I can try. The field still remains blank. Full script here:

cur_frm.cscript.validate = function (doc, cdt, cdn) {
  if (!cur_frm.doc.phone_number) {
    frappe.call({
      method: 'frappe.client.get_value',
      async: false,
      args: {
        doctype: 'Address',
        filters: [
      ['Dynamic Link', 'link_doctype', '=', 'Customer'],
      ['Dynamic Link', 'link_name', '=', doc.customer],
      ['address_type', '=', 'Billing']
    ],
    fieldname: 'phone'
      },
       callback: function (res) {
        if (res.message && res.message.phone) {
          frm.set_value(cdt, cdn, 'phone_number', res.message.phone);
        }
      }
    });
  }
};

@monojoker,

No need to pass the cdt, cdn if phone field is on parent form just use the frm.set_value(fieldname, value)

cdt, cdn is passed to set value if you want to set the value to child table field.

Thanks,
Makarand

Instead of adding separate custom field for phone number in the invoice, you can also embed the phone number in the address itself, if it works for you. For that you just need to change “Address Template”.

Thanks Nabin but this is for Sales Order. If there was a way to simply add the phone number from the address into the form using standard functionality that would be great. But I’m not sure there is?

Solution 1. The above filters suggested by Makarand does not work in get_value function. Instead of filtering based on customer and address_type, you can filter using address name, as it is present in all the sales transaction. So, filters will look like this:
filters: { "name": cur_frm.doc.customer_address }

Solution 2: Instead of adding custom field in the Sales Order, embed phone number in the address display. Modify the address template for that like this:

Hi Nabin, thanks for that I added it into the address template, it shows then on the SO Form. We also want to print JUST THAT PHONE NUMBER on a Custom Print Format from the Sales Order and include it in an auto-generated Email when a Sales Order reaches a particular Workflow_State. Do you know the syntax for retrieving the Address Template but only the last line (or a particular line based on the prefix text - eg “Phone:”)?

If not then I still need to get the custom script working. I tried the new Filter logic you pasted here but I must be entering it incorrectly as I get an error. This is my script:

//Below fetches the phone number from the Customer billing address record and writes it to the sales order form
cur_frm.cscript.validate = function (doc, cdt, cdn) {
  if (!cur_frm.doc.phone_number) {
    frappe.call({
      method: 'frappe.client.get_value',
      async: false,
      args: {
         doctype: 'Address',
         filters: { "name": cur_frm.doc.customer_address }
        fieldname: 'phone'
      },
      callback: function (res) {
         if (res.message && res.message.phone) {
          frm.set_value(cdt, cdn, 'phone_number', res.message.phone);
        }
      }
    });
  }
};

I also tried using “filters: [ “name”: cur_frm.doc.customer_address ]” as the old filters section used square brackets?

The same error appears when I try both:

SyntaxError: Unexpected token :
at Class.setup (https://mcleanimages.erpnext.com/assets/js/form.min.js:2625:17)
at _f.Frm.setup (https://mcleanimages.erpnext.com/assets/js/form.min.js:172:22)
at _f.Frm.refresh (https://mcleanimages.erpnext.com/assets/js/form.min.js:439:9)
at Class.load (https://mcleanimages.erpnext.com/assets/js/form.min.js:87:33)
at https://mcleanimages.erpnext.com/assets/js/form.min.js:82:7
at Object.callback (https://mcleanimages.erpnext.com/assets/js/desk.min.js:6616:6)
at Object.callback [as success_callback] (https://mcleanimages.erpnext.com/assets/js/desk.min.js:1204:16)
at _ (https://mcleanimages.erpnext.com/assets/js/desk.min.js:1228:34)
at Object. (https://mcleanimages.erpnext.com/assets/js/desk.min.js:1325:5)
at i (https://mcleanimages.erpnext.com/assets/frappe/js/lib/jquery/jquery.min.js:2:27151)

This error will appear if you use filters: [ "name": cur_frm.doc.customer_address ] because it has a syntax error.
But the above code should work. I hope you checked that customer_address is link field value is present in the Order.