test: test cases for PI and DN
diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py
index 4efe25a..d4f9aba 100644
--- a/erpnext/controllers/stock_controller.py
+++ b/erpnext/controllers/stock_controller.py
@@ -419,8 +419,8 @@
 					if fieldname and self.get(fieldname):
 						sl_dict[dimension.target_fieldname] = self.get(fieldname)
 
-				if sl_dict[dimension.target_fieldname]:
-					row.set(dimension.source_fieldname, sl_dict[dimension.target_fieldname])
+				if sl_dict[dimension.target_fieldname] and self.docstatus == 1:
+					row.db_set(dimension.source_fieldname, sl_dict[dimension.target_fieldname])
 
 	def make_sl_entries(self, sl_entries, allow_negative_stock=False, via_landed_cost_voucher=False):
 		from erpnext.stock.stock_ledger import make_sl_entries
diff --git a/erpnext/stock/doctype/inventory_dimension/inventory_dimension.js b/erpnext/stock/doctype/inventory_dimension/inventory_dimension.js
index 034f14e..79e7895 100644
--- a/erpnext/stock/doctype/inventory_dimension/inventory_dimension.js
+++ b/erpnext/stock/doctype/inventory_dimension/inventory_dimension.js
@@ -30,6 +30,7 @@
 
 	onload(frm) {
 		frm.trigger('render_traget_field');
+		frm.trigger("set_parent_fields");
 	},
 
 	refresh(frm) {
@@ -52,10 +53,6 @@
 		}
 	},
 
-	onload(frm) {
-		frm.trigger("set_parent_fields");
-	},
-
 	document_type(frm) {
 		frm.trigger("set_parent_fields");
 	},
@@ -76,7 +73,7 @@
 							[""].concat(r.message));
 					}
 				}
-			})
+			});
 		}
 	},
 
diff --git a/erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.py b/erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.py
index cc90b74..19ddc44 100644
--- a/erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.py
+++ b/erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.py
@@ -2,14 +2,17 @@
 # See license.txt
 
 import frappe
+from frappe.custom.doctype.custom_field.custom_field import create_custom_field
 from frappe.tests.utils import FrappeTestCase
 
+from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
 from erpnext.stock.doctype.inventory_dimension.inventory_dimension import (
 	CanNotBeChildDoc,
 	CanNotBeDefaultDimension,
 	DoNotChangeError,
 	delete_dimension,
 )
+from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
 from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
 from erpnext.stock.doctype.warehouse.test_warehouse import create_warehouse
 
@@ -136,6 +139,58 @@
 		self.assertTrue(inv_dim1.has_stock_ledger())
 		self.assertRaises(DoNotChangeError, inv_dim1.save)
 
+	def test_inventory_dimension_for_purchase_receipt_and_delivery_note(self):
+		create_inventory_dimension(
+			reference_document="Rack",
+			type_of_transaction="Both",
+			dimension_name="Rack",
+			apply_to_all_doctypes=1,
+			fetch_from_parent="Rack",
+		)
+
+		create_custom_field(
+			"Purchase Receipt", dict(fieldname="rack", label="Rack", fieldtype="Link", options="Rack")
+		)
+
+		create_custom_field(
+			"Delivery Note", dict(fieldname="rack", label="Rack", fieldtype="Link", options="Rack")
+		)
+
+		frappe.reload_doc("stock", "doctype", "purchase_receipt_item")
+		frappe.reload_doc("stock", "doctype", "delivery_note_item")
+
+		pr_doc = make_purchase_receipt(qty=2, do_not_submit=True)
+		pr_doc.rack = "Rack 1"
+		pr_doc.save()
+		pr_doc.submit()
+
+		pr_doc.load_from_db()
+
+		self.assertEqual(pr_doc.items[0].rack, "Rack 1")
+		sle_rack = frappe.db.get_value(
+			"Stock Ledger Entry",
+			{"voucher_detail_no": pr_doc.items[0].name, "voucher_type": pr_doc.doctype},
+			"rack",
+		)
+
+		self.assertEqual(sle_rack, "Rack 1")
+
+		dn_doc = create_delivery_note(qty=2, do_not_submit=True)
+		dn_doc.rack = "Rack 1"
+		dn_doc.save()
+		dn_doc.submit()
+
+		dn_doc.load_from_db()
+
+		self.assertEqual(dn_doc.items[0].rack, "Rack 1")
+		sle_rack = frappe.db.get_value(
+			"Stock Ledger Entry",
+			{"voucher_detail_no": dn_doc.items[0].name, "voucher_type": dn_doc.doctype},
+			"rack",
+		)
+
+		self.assertEqual(sle_rack, "Rack 1")
+
 
 def prepare_test_data():
 	if not frappe.db.exists("DocType", "Shelf"):
@@ -160,6 +215,28 @@
 
 	create_warehouse("Shelf Warehouse")
 
+	if not frappe.db.exists("DocType", "Rack"):
+		frappe.get_doc(
+			{
+				"doctype": "DocType",
+				"name": "Rack",
+				"module": "Stock",
+				"custom": 1,
+				"naming_rule": "By fieldname",
+				"autoname": "field:rack_name",
+				"fields": [{"label": "Rack Name", "fieldname": "rack_name", "fieldtype": "Data"}],
+				"permissions": [
+					{"role": "System Manager", "permlevel": 0, "read": 1, "write": 1, "create": 1, "delete": 1}
+				],
+			}
+		).insert(ignore_permissions=True)
+
+	for rack in ["Rack 1"]:
+		if not frappe.db.exists("Rack", rack):
+			frappe.get_doc({"doctype": "Rack", "rack_name": rack}).insert(ignore_permissions=True)
+
+	create_warehouse("Rack Warehouse")
+
 
 def create_inventory_dimension(**args):
 	args = frappe._dict(args)