Repository: SFDO-Tooling/CumulusCI
Pull request #3713 (2023-12-06 06:12:10)

Incorrect generation of record type mapping table name in a namespaced org context

[W-11242094](https://gus.lightning.force.com/lightning/r/ADM_Work__c/a07EE00000yQTZ8YAO/view)

In case of  `generate_and_load_from_yaml` when inject_namespace is true and while creating the record type table, the naming format doesn't actually support a case. 

That is it is searching for the namespace_sfobject_rt_mapping but the actually table created is sfobject_rt_mapping. 


Patch:
diff --git a/cumulusci/tasks/bulkdata/query_transformers.py b/cumulusci/tasks/bulkdata/query_transformers.py
index b3e636636..01a240465 100644
--- a/cumulusci/tasks/bulkdata/query_transformers.py
+++ b/cumulusci/tasks/bulkdata/query_transformers.py
@@ -103,16 +103,28 @@ class AddRecordTypesToQuery(LoadQueryExtender):
 
     @cached_property
     def outerjoins_to_add(self):
+
         if "RecordTypeId" in self.mapping.fields:
             try:
                 rt_source_table = self.metadata.tables[
                     self.mapping.get_source_record_type_table()
                 ]
+
             except KeyError as e:
-                raise BulkDataException(
-                    "A record type mapping table was not found in your dataset. "
-                    f"Was it generated by extract_data? {e}",
-                ) from e
+                # For generate_and_load_from_yaml, In case of namespace_inject true, mapping table name doesn't have namespace added
+                # We are checking for table_rt_mapping table
+                try:
+                    rt_source_table = self.metadata.tables[
+                        f"{self.mapping.table}_rt_mapping"
+                    ]
+
+                except KeyError as f:
+
+                    raise BulkDataException(
+                        "A record type mapping table was not found in your dataset. "
+                        f"Was it generated by extract_data? {e}",
+                    ) from f
+
             rt_dest_table = self.metadata.tables[
                 self.mapping.get_destination_record_type_table()
             ]


Test patch:
diff --git a/cumulusci/tasks/bulkdata/tests/recordtypes_2.sql b/cumulusci/tasks/bulkdata/tests/recordtypes_2.sql
new file mode 100644
index 000000000..8b3982984
--- /dev/null
+++ b/cumulusci/tasks/bulkdata/tests/recordtypes_2.sql
@@ -0,0 +1,19 @@
+BEGIN TRANSACTION;
+CREATE TABLE Beta_rt_mapping (
+	record_type_id VARCHAR(18) NOT NULL, 
+	developer_name VARCHAR(255), 
+	PRIMARY KEY (record_type_id)
+);
+INSERT INTO "Beta_rt_mapping" VALUES('012H40000003jCoIAI','recordtype2');
+INSERT INTO "Beta_rt_mapping" VALUES('012H40000003jCZIAY','recordtype1');
+CREATE TABLE Beta (
+	id INTEGER NOT NULL, 
+	"Name" VARCHAR(255), 
+	"RecordType" VARCHAR(255), 
+	PRIMARY KEY (id)
+);
+
+INSERT INTO "Beta" VALUES(15,'gamma12','012H40000003jCoIAI');
+INSERT INTO "Beta" VALUES(16,'gamma','012H40000003jCZIAY');
+INSERT INTO "Beta" VALUES(17,'gamma123','012H40000003jCZIAY');
+COMMIT;
diff --git a/cumulusci/tasks/bulkdata/tests/recordtypes_2.yml b/cumulusci/tasks/bulkdata/tests/recordtypes_2.yml
new file mode 100644
index 000000000..426c2ee50
--- /dev/null
+++ b/cumulusci/tasks/bulkdata/tests/recordtypes_2.yml
@@ -0,0 +1,6 @@
+Insert Account:
+    sf_object: Account
+    table: Beta
+    fields:
+        Name: Name
+        RecordTypeId: RecordType
diff --git a/cumulusci/tasks/bulkdata/tests/test_load.py b/cumulusci/tasks/bulkdata/tests/test_load.py
index 23ec22c54..4609a0320 100644
--- a/cumulusci/tasks/bulkdata/tests/test_load.py
+++ b/cumulusci/tasks/bulkdata/tests/test_load.py
@@ -1666,6 +1666,18 @@ FROM accounts LEFT OUTER JOIN accounts_sf_ids AS accounts_sf_ids_1 ON accounts_s
         """,
         )
 
+    def test_query_db__record_type_mapping_table_from_tablename(self):
+        _validate_query_for_mapping_step(
+            sql_path="cumulusci/tasks/bulkdata/tests/recordtypes_2.sql",
+            mapping="cumulusci/tasks/bulkdata/tests/recordtypes_2.yml",
+            mapping_step_name="Insert Account",
+            expected="""SELECT "Beta".id AS "Beta_id", "Beta"."Name" AS "Beta_Name", "Account_rt_target_mapping".record_type_id AS "Account_rt_target_mapping_record_type_id"
+            FROM "Beta"
+            LEFT OUTER JOIN "Beta_rt_mapping" ON "Beta_rt_mapping".record_type_id = "Beta"."RecordType"
+            LEFT OUTER JOIN "Account_rt_target_mapping" ON "Account_rt_target_mapping".developer_name = "Beta_rt_mapping".developer_name
+        """,
+        )
+
     @mock.patch("cumulusci.tasks.bulkdata.load.automap_base")
     @responses.activate
     def test_init_db__record_type_mapping(self, base):