diff --git a/cumulusci/tasks/bulkdata/query_transformers.py b/cumulusci/tasks/bulkdata/query_transformers.py index b3e636636b..01a240465d 100644 --- a/cumulusci/tasks/bulkdata/query_transformers.py +++ b/cumulusci/tasks/bulkdata/query_transformers.py @@ -103,16 +103,28 @@ def filters_to_add(self): @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() ] diff --git a/cumulusci/tasks/bulkdata/tests/recordtypes_2.sql b/cumulusci/tasks/bulkdata/tests/recordtypes_2.sql new file mode 100644 index 0000000000..8b39829847 --- /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 0000000000..426c2ee50e --- /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 23ec22c543..4609a0320d 100644 --- a/cumulusci/tasks/bulkdata/tests/test_load.py +++ b/cumulusci/tasks/bulkdata/tests/test_load.py @@ -1666,6 +1666,18 @@ def test_query_db__record_type_mapping(self): """, ) + 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):