Skip to content

Commit cfeaf34

Browse files
committed
feat: new valueSource prop in core:RecordsTransform module
1 parent 45df2e8 commit cfeaf34

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"repository": "forcedotcom/SFDX-Data-Move-Utility",
8181
"addons": {
8282
"run": {
83-
"version": "1.5.0"
83+
"version": "1.6.0"
8484
}
8585
},
8686
"scripts": {

src/addons/modules/sfdmu-run/RecordsTransform/index.ts

+31-14
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ interface IField {
4444
isConstant: boolean;
4545
lookupExpression: string;
4646
lookupSource: 'source' | 'target';
47+
valueSource: 'source' | 'target';
4748

4849
// Runtime ------
4950
sourceTask: SfdmuRunAddonTask,
5051
lookupFieldMap: Map<string, string[]>, // referenced object => child id fields
5152
constantValue: any;
53+
targetToSourceRecordMap: Map<any, any>
5254
}
5355

5456
interface IOnExecuteArguments {
@@ -239,11 +241,14 @@ export default class RecordsTransform extends SfdmuRunAddonModule {
239241
field.sourceObject);
240242
} else {
241243
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));
245248
}
246-
249+
task.sourceToTargetRecordMap.forEach((targetRecord, sourceRecord) => {
250+
fieldsMap.get(field.alias).targetToSourceRecordMap.set(targetRecord, sourceRecord);
251+
});
247252
}
248253
}
249254
});
@@ -279,38 +284,50 @@ export default class RecordsTransform extends SfdmuRunAddonModule {
279284

280285
let formula = {};
281286

287+
let transformationSourceRecord = transformation.targetTask.sourceTaskData.idRecordsMap.get(transformedRecord["___Id"]);
288+
let tranformationTargetRecord = transformation.targetTask.sourceToTargetRecordMap.get(transformationSourceRecord);
289+
282290
// Generate formula properties for further eval() on the targetRecord
283291
fieldsMap.forEach((field: IField) => {
284292

285293
let sourceRecords = field.sourceTask.sourceTaskData.records;
286294
let targetRecords = field.sourceTask.targetTaskData.records;
287295

296+
let sourceRecord = field.sourceTask.sourceTaskData.idRecordsMap.get(transformedRecord["___Id"]);
297+
288298
if (transformation.targetObject == field.sourceObject || !!field.lookupExpression) {
289299
// 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);
291303
} else {
292304
// Different object => lookup based transformation
293305
let sourceIdFields = transformation.lookupFieldMap.get(field.sourceObject);// [Account.Country__c]
294306
if (sourceIdFields) {
295307
// Target ==> Source: selecting the source record using the target lookup to the source record (Account.Country__c => Country Id)
296308
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);
300314
});
301315
} else {
302316
let targetIdFields = field.lookupFieldMap.get(transformation.targetObject); // [Country__c.BusinessAccount__c]
303317
if (targetIdFields) {
304318
// 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"];
306320
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
311326
return sourceRecord[fieldName] == targetId;
312327
})) {
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);
314331
break;
315332
}
316333
}

0 commit comments

Comments
 (0)