Try ERPNext Buy Support Partners Foundation

I need a script for an address field for purchase orders and Invoices

Hello, I need to figure out a way to populate multiple fields on a purchase order for delivery purposes.

We do a lot of drop shipping directly to the customer (which is fairly standard in my industry), and we need a way to have the shipping address on the PO’s so that the Supplier knows where to ship the items.

I could create a custom small text field and just type the address in manually each time I need to drop-ship and item, but that seems to be too much work for something that a little bit of scripting (or perhaps HTML formatting in the print format) should be able to automate. Essentially, I want the Purchase Orders to treat addresses similar to how the Delivery Notes do, in that the user selects one address field, and the full address is then populated into a field that is printed on the document. To be frank, I am a bit surprised to see that this doesn’t behave this way currently, but maybe my industry is just an oddity and it’s a feature that few people take advantage of.

Based on what I have read, a Fetch from Master script should do the trick, but I do not know the propper syntax to do this multiple times throughout the document:

Here are the fields I am working with:

Field 1: Address Name - Master (link to customer address)
Field 2: Address 1
Field 3: Address 2
Field 4: City
Field 5: State
Field 6: Postal Code

The script that I found in the manual is the following:

add_fetch(link_fieldname, source_fieldname, target_fieldname)

Using this as a starting point, I have managed to populate one of these fields, but I do not know the proper syntax to populate the rest of them. Also, I have a feeling that I can probably consolidate fields 2-6 into a Small Text field, but again, I would need to know the syntax for bringing all the information into it.

If anyone is willing to help, I would appreciate it.

Condensed Version: I need a way to target multiple fields with multiple sources from a single linked field by using a custom script, or another method if it is easier.

I did find this post from awhile back that I believe should do what I want it to do, but unfortunately, I could not get it to work. If anyone would not mind a brief walkthrough, I would be super grateful.

standard solution is to create 2 field

  1. link with address (address title)
  2. store detail address (text field)

When you change address, fetch detail address into detail address text field

sample code is

frappe.ui.form.on("Purchase Order", "address_title", function(frm, cdt, cdn) {
        return frm.call({
            method: "erpnext.utilities.doctype.address.address.get_address_display",
            child: d,
            args: {
                "address_dict": frm.doc.address_title
            },
                        callback: function(r) {
            if(r.message)
                                frappe.model.set_value("detail_address", r.message);
        }
        });
     }
})

Thanks. I set the form up according to your directions, and copied the script into a new custom script. Now whenever I load the form, all of the fields are invisible. They are still there, because I can see them reverenced in Chrome’s Inspector, but I just cannot see or interact with them. Any ideas?

Could someone maybe chime in and point me in the right direction. For whatever reason, when I create a custom script for the Purchase Order DocType, and use the script that kolate_sambhaji provided above, all the fields are blank. Here is an image to show what I mean.

Here is what the form looks like without the script:

Any help would be appreciated. It is essential that we have the ability to include Shipping addresses on our PO’s.

Bumping for exposure.

The reason it does this is because there is an error in your script.
I would suggest the following things to try:

Check to make sure you created the address_title and detail_address fields correctly.
(a) the address_title field is a Link with the Option set to Address
(b) The detail_address field is a Small Text field

Also, I noticed the brackets don’t match up in the script provided. Try this:

frappe.ui.form.on(“Purchase Order”, “address_title”, function(frm, cdt, cdn) {
return frm.call({
method: “erpnext.utilities.doctype.address.address.get_address_display”,
child: d,
args: {
“address_dict”: frm.doc.address_title
},
callback: function® {
if(r.message)
frappe.model.set_value(“detail_address”, r.message);
}
});
});

Thanks! The form loads correctly now, but unfortunately, the script doesn’t populate the detail_address field.

Just to confirm, I have two custom fields named address_title and detail_address.

address_title is a link field with “Address” under options.

detail_address is set as Small Text.

I copied and pasted the script into the Custom Script form and set the DocType as “Purchase Order.”

Is there anything else that I may have missed?

I believe all you should need to do is select the address in the address_title field from your form.

If you are just going to populate it with the address of the supplier, you should be able to do that automatically by using this script:

cur_frm.cscript.custom_onload = function () {
	if (cur_frm.doc.supplier) {
		frappe.call({
            "method": "frappe.client.get",
            args: {
                doctype: "Supplier",
                name: cur_frm.doc.supplier
            },
            callback: function (data) {
                frappe.model.set_value(cur_frm.doctype,cur_frm.docname, "address_title ",
                    data.message.address)
                    
            }
        })
	}
}

That way as soon as the script loads, it will figure out what the suppliers address is and fill it in. If you don’t have a supplier in the script right at the start, you may need to fill in the box manually (i.e. type in the “name” of the supplier address). You can make that easier by using this code:

cur_frm.fields_dict['address_title'].get_query = function(doc, cdt, cdn) {
	return {
		filters: {'supplier': doc.supplier}
	}
}

Hopefully that works for you.

Actually, it is the customer’s shipping address that I am trying to fill in. We do quite a bit of drop shipping which is why I am trying to achieve this functionality for PO’s. I’m going to tweak the script a little and see if that works. Thanks!!!

Oh, and just to be clear, I was indeed selecting the customer’s address in the drop down, but was unsuccessful at getting the address to populate to the detail_address field.