Fetch a complete address in custom field of Purchase Order

I wanted to add a “Bill To” address to Purchase Order. When customizing the PO form a came across this field company which does not show on the PO. Can any one clarify why is it not seen in the PO ?

Also, I believe I can add an additional field here which can be populated with one of the company address which will act as billing address. Is this the correct way of doing it ?

Also, a comment on ERPNext PO design, the shipping address is so filtered in ERPNext to be populated only with ones company’s addresses. That should actually be billing address. The shipping address should be left free, One may always desire to get the items on site or at sub-contractors site.

Regards
PK

1 Like

I may be wrong with this, but the company field will show up if there are multiple companies created in that ERPNext instance.

  1. Create New Address

Step 2:
If Preferred Billing Address is marked, the same will be populated by default for billing address.
If Preferred Shipping Address is marked, the same will be populated by default for shipping address.

Step 3: Mark Is Your Company Address

Step 4: Add the link to the company.

Thanks for the quick reply.
I added a preferred billing address. However that populates automatically the shipping address space on the PO.

It does not create an additional address on the PO different from the shipping address which is the billing address.

Did I miss something ?

Well I did add the additional fields. Used the standard process of adding custom fields to a form
Added two new fields

  1. Select To Ship Address
  2. To Ship Address (Read Only)

The first item is a link and works just fine
The second item (small text) does not auto-populate like other address in the Purchase Order

I believe we need to fill the “Fetch from” field of that item. However, the address is multi line. How can I fetch the multi line address from “Select To Ship Address” link ?

So it’s either a bug, or the behaviour is that if no shipping/preferred shipping is marked, it will pull the billing address.

This needs to be validated

I am quite sure that is the behaviour.

However I need help to fetch address using a custom script. I have gone through several links where different answers to this question have been posted but they have not seemed to work for me.

I need an answer to the the same question asked here but the answer seems outdated,

Also, a variant of that is present here.

I have so far not been able to pull the complete address when the address name is selected. A simple fetch will not work because the address is made up of multiple lines.

Look forward to this and I see that this is such a typical question that its answer should go in the custom script examples.

Regards

I used the following code to achieve fetching address to a read only custom field when the address name is selected

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

When I select an address in the “to_ship_address” field I get an error message.

Wrong fieldname address in add_fetch configuration of custom script

What went wrong ?

This is what was wrong in it. I had already used a code in the fetch “fetch from” area of customise form and used the word address in it. Since the address doc type does not have anything called address that was the cause of error.

However fixing it did not achieve for me what I wanted. Instead I get an message

The resource you are looking for is not available

What did I miss ?

I got the solution. The following is the code that worked.

Note that
erpnext.utilities.doctype.address.address.get_address_display
seen in answers till v8 is replaced by
frappe.contacts.doctype.address.address.get_address_display

frappe.ui.form.on("Purchase Order", "to_ship_address", function(frm, cdt, cdn) {
	return frm.call({
		method: :frappe.contacts.doctype.address.address.get_address_display",
		args: {
			"address_dict": frm.doc.address_title
		},
		callback: function(r) {
			if(r.message)
				frm.set_value("to_ship_address_display", r.message);
		}
	});
});

Has there been a change in the utilities available ?

A mild improvisation to remove the address_display when the address selection is removed.

frappe.ui.form.on("Purchase Order", "to_ship_address", function(frm, cdt, cdn) {
    if(frm.doc.to_ship_address){
    	return frm.call({
    		method: "frappe.contacts.doctype.address.address.get_address_display",
    		args: {
    			"address_dict": frm.doc.to_ship_address
    		},
    		callback: function(r) {
    			if(r.message)
    				frm.set_value("to_ship_address_display", r.message);
    		}
    	});
    }
    else{
        frm.set_value("to_ship_address_display", "");
    }
});
3 Likes

@pkrulz , a better solution now is v13 for new POs. However, it has a minor bug when generating a PO from a SO, which can be solved using a client side script, described in this github issue.

The script is:

frappe.ui.form.on('Purchase Order', {
	refresh(frm) {
		frappe.call({
			'method': 'frappe.contacts.doctype.address.address.get_default_address',
			'args': {
				'doctype': 'Company',
				'name': frm.doc.company
			},
			'callback': function(r) {
				frm.set_value('billing_address', r.message);
			}
		});
	}
});
1 Like