@@ -310,6 +310,90 @@ def _execute_step(
310310
311311 return step .job_result
312312
313+ def process_lookup_fields (self , mapping , fields , polymorphic_fields ):
314+ """Modify fields and priority fields based on lookup and polymorphic checks."""
315+ for name , lookup in mapping .lookups .items ():
316+ if name in fields :
317+ # Get the index of the lookup field before removing it
318+ insert_index = fields .index (name )
319+ # Remove the lookup field from fields
320+ fields .remove (name )
321+
322+ # Do the same for priority fields
323+ lookup_in_priority_fields = False
324+ if name in mapping .select_options .priority_fields :
325+ # Set flag to True
326+ lookup_in_priority_fields = True
327+ # Remove the lookup field from priority fields
328+ del mapping .select_options .priority_fields [name ]
329+
330+ # Check if this lookup field is polymorphic
331+ if (
332+ name in polymorphic_fields
333+ and len (polymorphic_fields [name ]["referenceTo" ]) > 1
334+ ):
335+ # Convert to list if string
336+ if not isinstance (lookup .table , list ):
337+ lookup .table = [lookup .table ]
338+ # Polymorphic field handling
339+ polymorphic_references = lookup .table
340+ relationship_name = polymorphic_fields [name ]["relationshipName" ]
341+
342+ # Loop through each polymorphic type (e.g., Contact, Lead)
343+ for ref_type in polymorphic_references :
344+ # Find the mapping step for this polymorphic type
345+ lookup_mapping_step = next (
346+ (
347+ step
348+ for step in self .mapping .values ()
349+ if step .table == ref_type
350+ ),
351+ None ,
352+ )
353+ if lookup_mapping_step :
354+ lookup_fields = lookup_mapping_step .get_load_field_list ()
355+ # Insert fields in the format {relationship_name}.{ref_type}.{lookup_field}
356+ for field in lookup_fields :
357+ fields .insert (
358+ insert_index ,
359+ f"{ relationship_name } .{ lookup_mapping_step .sf_object } .{ field } " ,
360+ )
361+ insert_index += 1
362+ if lookup_in_priority_fields :
363+ mapping .select_options .priority_fields [
364+ f"{ relationship_name } .{ lookup_mapping_step .sf_object } .{ field } "
365+ ] = f"{ relationship_name } .{ lookup_mapping_step .sf_object } .{ field } "
366+
367+ else :
368+ # Non-polymorphic field handling
369+ lookup_table = lookup .table
370+
371+ if isinstance (lookup_table , list ):
372+ lookup_table = lookup_table [0 ]
373+
374+ # Get the mapping step for the non-polymorphic reference
375+ lookup_mapping_step = next (
376+ (
377+ step
378+ for step in self .mapping .values ()
379+ if step .table == lookup_table
380+ ),
381+ None ,
382+ )
383+
384+ if lookup_mapping_step :
385+ relationship_name = polymorphic_fields [name ]["relationshipName" ]
386+ lookup_fields = lookup_mapping_step .get_load_field_list ()
387+
388+ # Insert the new fields at the same position as the removed lookup field
389+ for field in lookup_fields :
390+ fields .insert (insert_index , f"{ relationship_name } .{ field } " )
391+ insert_index += 1
392+ if lookup_in_priority_fields :
393+ mapping .select_options .priority_fields [
394+ f"{ relationship_name } .{ field } "
395+ ] = f"{ relationship_name } .{ field } "
396+
313397 def configure_step (self , mapping ):
314398 """Create a step appropriate to the action"""
315399 bulk_mode = mapping .bulk_mode or self .bulk_mode or "Parallel"
@@ -370,85 +454,7 @@ def configure_step(self, mapping):
370454 for field in describe_result ["fields" ]
371455 if field ["type" ] == "reference"
372456 }
373-
374- # Loop through each lookup to get the corresponding fields
375- for name , lookup in mapping .lookups .items ():
376- if name in fields :
377- # Get the index of the lookup field before removing it
378- insert_index = fields .index (name )
379- # Remove the lookup field from fields
380- fields .remove (name )
381-
382- # Check if this lookup field is polymorphic
383- if (
384- name in polymorphic_fields
385- and len (polymorphic_fields [name ]["referenceTo" ]) > 1
386- ):
387- # Convert to list if string
388- if not isinstance (lookup .table , list ):
389- lookup .table = [lookup .table ]
390- # Polymorphic field handling
391- polymorphic_references = lookup .table
392- relationship_name = polymorphic_fields [name ][
393- "relationshipName"
394- ]
395-
396- # Loop through each polymorphic type (e.g., Contact, Lead)
397- for ref_type in polymorphic_references :
398- # Find the mapping step for this polymorphic type
399- lookup_mapping_step = next (
400- (
401- step
402- for step in self .mapping .values ()
403- if step .sf_object == ref_type
404- ),
405- None ,
406- )
407-
408- if lookup_mapping_step :
409- lookup_fields = (
410- lookup_mapping_step .get_load_field_list ()
411- )
412- # Insert fields in the format {relationship_name}.{ref_type}.{lookup_field}
413- for field in lookup_fields :
414- fields .insert (
415- insert_index ,
416- f"{ relationship_name } .{ lookup_mapping_step .sf_object } .{ field } " ,
417- )
418- insert_index += 1
419-
420- else :
421- # Non-polymorphic field handling
422- lookup_table = lookup .table
423-
424- if isinstance (lookup_table , list ):
425- lookup_table = lookup_table [0 ]
426-
427- # Get the mapping step for the non-polymorphic reference
428- lookup_mapping_step = next (
429- (
430- step
431- for step in self .mapping .values ()
432- if step .sf_object == lookup_table
433- ),
434- None ,
435- )
436-
437- if lookup_mapping_step :
438- relationship_name = polymorphic_fields [name ][
439- "relationshipName"
440- ]
441- lookup_fields = (
442- lookup_mapping_step .get_load_field_list ()
443- )
444-
445- # Insert the new fields at the same position as the removed lookup field
446- for field in lookup_fields :
447- fields .insert (
448- insert_index , f"{ relationship_name } .{ field } "
449- )
450- insert_index += 1
451-
457+ self .process_lookup_fields (mapping , fields , polymorphic_fields )
452458 else :
453459 action = mapping .action
454460
@@ -503,9 +509,6 @@ def _stream_queried_data(self, mapping, local_ids, query):
503509 pkey = row [0 ]
504510 row = list (row [1 :]) + statics
505511
506- # Replace None values in row with empty strings
507- row = [value if value is not None else "" for value in row ]
508-
509512 if mapping .anchor_date and (date_context [0 ] or date_context [1 ]):
510513 row = adjust_relative_dates (
511514 mapping , date_context , row , DataOperationType .INSERT
0 commit comments