@@ -312,10 +312,14 @@ def _execute_step(
312312
313313 def process_lookup_fields (self , mapping , fields , polymorphic_fields ):
314314 """Modify fields and priority fields based on lookup and polymorphic checks."""
315+ # Store the lookups and their original order for re-insertion at the end
316+ original_lookups = [name for name in fields if name in mapping .lookups ]
317+ max_insert_index = - 1
315318 for name , lookup in mapping .lookups .items ():
316319 if name in fields :
317320 # Get the index of the lookup field before removing it
318321 insert_index = fields .index (name )
322+ max_insert_index = max (max_insert_index , insert_index )
319323 # Remove the lookup field from fields
320324 fields .remove (name )
321325
@@ -351,14 +355,15 @@ def process_lookup_fields(self, mapping, fields, polymorphic_fields):
351355 None ,
352356 )
353357 if lookup_mapping_step :
354- lookup_fields = lookup_mapping_step .get_load_field_list ()
358+ lookup_fields = lookup_mapping_step .fields . keys ()
355359 # Insert fields in the format {relationship_name}.{ref_type}.{lookup_field}
356360 for field in lookup_fields :
357361 fields .insert (
358362 insert_index ,
359363 f"{ relationship_name } .{ lookup_mapping_step .sf_object } .{ field } " ,
360364 )
361365 insert_index += 1
366+ max_insert_index = max (max_insert_index , insert_index )
362367 if lookup_in_priority_fields :
363368 mapping .select_options .priority_fields [
364369 f"{ relationship_name } .{ lookup_mapping_step .sf_object } .{ field } "
@@ -383,17 +388,24 @@ def process_lookup_fields(self, mapping, fields, polymorphic_fields):
383388
384389 if lookup_mapping_step :
385390 relationship_name = polymorphic_fields [name ]["relationshipName" ]
386- lookup_fields = lookup_mapping_step .get_load_field_list ()
391+ lookup_fields = lookup_mapping_step .fields . keys ()
387392
388393 # Insert the new fields at the same position as the removed lookup field
389394 for field in lookup_fields :
390395 fields .insert (insert_index , f"{ relationship_name } .{ field } " )
391396 insert_index += 1
397+ max_insert_index = max (max_insert_index , insert_index )
392398 if lookup_in_priority_fields :
393399 mapping .select_options .priority_fields [
394400 f"{ relationship_name } .{ field } "
395401 ] = f"{ relationship_name } .{ field } "
396402
403+ # Append the original lookups at the end in the same order
404+ for name in original_lookups :
405+ if name not in fields :
406+ fields .insert (max_insert_index , name )
407+ max_insert_index += 1
408+
397409 def configure_step (self , mapping ):
398410 """Create a step appropriate to the action"""
399411 bulk_mode = mapping .bulk_mode or self .bulk_mode or "Parallel"
@@ -479,6 +491,7 @@ def configure_step(self, mapping):
479491 selection_filter = mapping .select_options .filter ,
480492 selection_priority_fields = mapping .select_options .priority_fields ,
481493 content_type = content_type ,
494+ threshold = mapping .select_options .threshold ,
482495 )
483496 return step , query
484497
@@ -588,10 +601,9 @@ def _query_db(self, mapping):
588601 mapping , self .mapping , self .metadata , model , self ._old_format
589602 )
590603 )
591- else :
592- transformers .append (
593- AddLookupsToQuery (mapping , self .metadata , model , self ._old_format )
594- )
604+ transformers .append (
605+ AddLookupsToQuery (mapping , self .metadata , model , self ._old_format )
606+ )
595607
596608 transformers .extend ([cls (mapping , self .metadata , model ) for cls in classes ])
597609
0 commit comments