@@ -44,11 +44,13 @@ interface IField {
44
44
isConstant : boolean ;
45
45
lookupExpression : string ;
46
46
lookupSource : 'source' | 'target' ;
47
+ valueSource : 'source' | 'target' ;
47
48
48
49
// Runtime ------
49
50
sourceTask : SfdmuRunAddonTask ,
50
51
lookupFieldMap : Map < string , string [ ] > , // referenced object => child id fields
51
52
constantValue : any ;
53
+ targetToSourceRecordMap : Map < any , any >
52
54
}
53
55
54
56
interface IOnExecuteArguments {
@@ -239,11 +241,14 @@ export default class RecordsTransform extends SfdmuRunAddonModule {
239
241
field . sourceObject ) ;
240
242
} else {
241
243
fieldsMap . set ( field . alias , Object . assign ( field , {
242
- sourceTask : task ,
243
- lookupFieldMap : __getLookups ( task . fieldsInQueryMap )
244
- } ) ) ;
244
+ sourceTask : task ,
245
+ lookupFieldMap : __getLookups ( task . fieldsInQueryMap ) ,
246
+ targetToSourceRecordMap : new Map < any , any > ( )
247
+ } as IField ) ) ;
245
248
}
246
-
249
+ task . sourceToTargetRecordMap . forEach ( ( targetRecord , sourceRecord ) => {
250
+ fieldsMap . get ( field . alias ) . targetToSourceRecordMap . set ( targetRecord , sourceRecord ) ;
251
+ } ) ;
247
252
}
248
253
}
249
254
} ) ;
@@ -279,38 +284,50 @@ export default class RecordsTransform extends SfdmuRunAddonModule {
279
284
280
285
let formula = { } ;
281
286
287
+ let transformationSourceRecord = transformation . targetTask . sourceTaskData . idRecordsMap . get ( transformedRecord [ "___Id" ] ) ;
288
+ let tranformationTargetRecord = transformation . targetTask . sourceToTargetRecordMap . get ( transformationSourceRecord ) ;
289
+
282
290
// Generate formula properties for further eval() on the targetRecord
283
291
fieldsMap . forEach ( ( field : IField ) => {
284
292
285
293
let sourceRecords = field . sourceTask . sourceTaskData . records ;
286
294
let targetRecords = field . sourceTask . targetTaskData . records ;
287
295
296
+ let sourceRecord = field . sourceTask . sourceTaskData . idRecordsMap . get ( transformedRecord [ "___Id" ] ) ;
297
+
288
298
if ( transformation . targetObject == field . sourceObject || ! ! field . lookupExpression ) {
289
299
// Same object => direct transformation (formula[accountCategory] = Account.Category__c)
290
- __setFormulaValue ( formula , transformedRecord , transformedRecord , field , sourceRecords , targetRecords ) ;
300
+ let targetRecord = field . sourceTask . sourceToTargetRecordMap . get ( sourceRecord ) ;
301
+ const tempSourceRecord = field . valueSource != 'target' ? sourceRecord : targetRecord ;
302
+ __setFormulaValue ( formula , tempSourceRecord , transformedRecord , field , sourceRecords , targetRecords ) ;
291
303
} else {
292
304
// Different object => lookup based transformation
293
305
let sourceIdFields = transformation . lookupFieldMap . get ( field . sourceObject ) ; // [Account.Country__c]
294
306
if ( sourceIdFields ) {
295
307
// Target ==> Source: selecting the source record using the target lookup to the source record (Account.Country__c => Country Id)
296
308
sourceIdFields . forEach ( sourceIdField => {
297
- let sourceId = transformedRecord [ sourceIdField ] ;
298
- let sourceRecord = field . lookupSource == 'source' ? field . sourceTask . sourceTaskData . idRecordsMap . get ( sourceId ) : field . sourceTask . targetTaskData . idRecordsMap . get ( sourceId ) ;
299
- __setFormulaValue ( formula , sourceRecord , transformedRecord , field , sourceRecords , targetRecords ) ;
309
+ let targetId = transformedRecord [ sourceIdField ] ; // Its always the Id field on the target side because the transformed records is ready to be inserted or
310
+ let targetRecord = field . sourceTask . targetTaskData . idRecordsMap . get ( targetId ) ;
311
+ sourceRecord = field . targetToSourceRecordMap . get ( targetRecord ) ;
312
+ const tempSourceRecord = field . valueSource != 'target' ? sourceRecord : targetRecord ;
313
+ __setFormulaValue ( formula , tempSourceRecord , transformedRecord , field , sourceRecords , targetRecords ) ;
300
314
} ) ;
301
315
} else {
302
316
let targetIdFields = field . lookupFieldMap . get ( transformation . targetObject ) ; // [Country__c.BusinessAccount__c]
303
317
if ( targetIdFields ) {
304
318
// Source ==> Target: selecting the source record using source lookup to the target record (Country__c.BusinessAccount__c => Account.Id => Country Id
305
- let targetId = transformedRecord [ "Id" ] ;
319
+ let targetId = tranformationTargetRecord [ "Id" ] ;
306
320
if ( targetId ) {
307
- // Find all the source records which is pointing to the current targetId
308
- for ( let index = 0 ; index < sourceRecords . length ; index ++ ) {
309
- const sourceRecord = sourceRecords [ index ] ;
310
- if ( Object . keys ( sourceRecord ) . some ( fieldName => {
321
+ // Find all the target records which is pointing to the current targetId
322
+ for ( let index = 0 ; index < targetRecords . length ; index ++ ) {
323
+ let targetRecord = targetRecords [ index ] ;
324
+ if ( Object . keys ( targetRecord ) . some ( fieldName => {
325
+ // Check if any lookup field on the target record is pointing to the current targetId
311
326
return sourceRecord [ fieldName ] == targetId ;
312
327
} ) ) {
313
- __setFormulaValue ( formula , sourceRecord , transformedRecord , field , sourceRecords , targetRecords ) ;
328
+ sourceRecord = field . targetToSourceRecordMap . get ( targetRecord ) ;
329
+ const tempSourceRecord = field . valueSource != 'target' ? sourceRecord : targetRecord ;
330
+ __setFormulaValue ( formula , tempSourceRecord , transformedRecord , field , sourceRecords , targetRecords ) ;
314
331
break ;
315
332
}
316
333
}
0 commit comments