optimize patch for faster execution (#13068)

diff --git a/erpnext/patches/v10_0/update_territory_and_customer_group.py b/erpnext/patches/v10_0/update_territory_and_customer_group.py
index 52a6db2..56152f9 100644
--- a/erpnext/patches/v10_0/update_territory_and_customer_group.py
+++ b/erpnext/patches/v10_0/update_territory_and_customer_group.py
@@ -1,13 +1,13 @@
 import frappe
-from frappe.model.rename_doc import update_linked_doctypes
+from frappe.model.rename_doc import update_linked_doctypes, get_fetch_fields
 
 def execute():
-	customers = frappe.get_all('Customer')
+	customers = frappe.get_all('Customer', fields=["name", "territory", "customer_group"])
+	territory_fetch = get_fetch_fields('Customer', 'Territory')
+	customer_group_fetch = get_fetch_fields('Customer', 'Customer Group')
+
 	for customer in customers:
 		# Update Territory across all transaction
-		terr = frappe.get_value('Customer', customer, 'territory')
-		update_linked_doctypes("Customer", "Territory", customer.name, terr)
-
+		update_linked_doctypes(territory_fetch, customer.name, customer.territory_value)
 		# Update Territory across all transaction
-		cust_group = frappe.get_value('Customer', customer, 'customer_group')
-		update_linked_doctypes("Customer", "Customer Group", customer.name, cust_group)
+		update_linked_doctypes(customer_group_fetch, customer.name, customer.customer_group_value)
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index c550d06..761c017 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -11,7 +11,7 @@
 from erpnext.utilities.transaction_base import TransactionBase
 from erpnext.accounts.party import validate_party_accounts, get_dashboard_info, get_timeline_data # keep this
 from frappe.contacts.address_and_contact import load_address_and_contact, delete_contact_and_address
-from frappe.model.rename_doc import update_linked_doctypes
+from frappe.model.rename_doc import update_linked_doctypes, get_fetch_fields
 
 class Customer(TransactionBase):
 	def get_feed(self):
@@ -57,13 +57,13 @@
 		self.check_customer_group_or_territory_change()
 
 	def check_customer_group_or_territory_change(self):
-		frappe.flags.customer_group, frappe.flags.territory = False, False
+		frappe.flags.customer_group_changed, frappe.flags.territory_changed = False, False
 
 		if not self.get('__islocal'):
 			if self.customer_group != frappe.db.get_value('Customer', self.name, 'customer_group'):
-				frappe.flags.customer_group = True
+				frappe.flags.customer_group_changed = True
 			if self.territory != frappe.db.get_value('Customer', self.name, 'territory'):
-				frappe.flags.territory = True
+				frappe.flags.territory_changed = True
 
 	def on_update(self):
 		self.validate_name_with_customer_group()
@@ -76,10 +76,12 @@
 		if self.flags.is_new_doc:
 			self.create_lead_address_contact()
 
-		if frappe.flags.territory:
-			update_linked_doctypes("Customer", "Territory", self.name, self.territory)
-		if frappe.flags.customer_group:
-			update_linked_doctypes("Customer", "Customer Group", self.name, self.customer_group)
+		if frappe.flags.territory_changed:
+			territory_fetch = get_fetch_fields('Customer', 'Territory')
+			update_linked_doctypes(territory_fetch, self.name, self.territory)
+		if frappe.flags.customer_group_changed:
+			customer_group_fetch = get_fetch_fields('Customer', 'Customer Group')
+			update_linked_doctypes(customer_group_fetch, self.name, self.customer_group)
 
 	def create_primary_contact(self):
 		if not self.customer_primary_contact and not self.lead_name: