diff --git a/src/commands/texei/data/export.ts b/src/commands/texei/data/export.ts index 22e22d9..c38ab27 100644 --- a/src/commands/texei/data/export.ts +++ b/src/commands/texei/data/export.ts @@ -11,6 +11,7 @@ interface DataPlan { label: string; filters: string; excludedFields: Array; + exportOwner: boolean; } // Initialize Messages with the current plugin directory @@ -122,10 +123,9 @@ export default class Export extends SfdxCommand { if (field.createable && !fieldsToExclude.includes(field.name)) { fields.push(field.name); - // If it's a lookup, also add it to the lookup list, to be replaced later - // Excluding OwnerId as we are not importing users anyway - if (field.referenceTo && field.referenceTo.length > 0 && field.name != 'OwnerId' && field.name != 'RecordTypeId') { + // If it's a lookup, also add it to the lookup list, to be replaced later + if (field.referenceTo && field.referenceTo.length > 0 && field.name != 'RecordTypeId' && field.name != 'OwnerId') { // If User is queried, use the reference, otherwise use the Scratch Org User if (!objectList.find(x => x.name === 'User') && field.referenceTo.includes('User')) { userFieldsReference.push(field.name); @@ -228,7 +228,11 @@ export default class Export extends SfdxCommand { // Delete unused fields delete record.Id; delete record.RecordType; - delete record.OwnerId; + + // Remove Owner Id if we haven't explicitely requested it in the plan + if (!sobject.exportOwner) { + delete record.OwnerId; + } if (sobject.name === 'Pricebook2') { delete record.IsStandard; diff --git a/src/commands/texei/data/import.ts b/src/commands/texei/data/import.ts index 405b08c..9ac3df9 100644 --- a/src/commands/texei/data/import.ts +++ b/src/commands/texei/data/import.ts @@ -100,6 +100,9 @@ export default class Import extends SfdxCommand { const lookups: Array = await this.getLookupsForObject(sobjectName); let recTypeInfos = new Map(); + // Get list of valid user ids in the org + const userSet = await this.getValidUserAndQueueList(); + // Get Record Types information with newly generated Ids recTypeInfos = await this.getRecordTypeMap(sobjectName); @@ -119,6 +122,11 @@ export default class Import extends SfdxCommand { } } + // Remove OwnerId if the user is not found and active in the org + if (!userSet.has(sobject.OwnerId)) { + delete sobject.OwnerId; + } + // Replace Record Types, if any if (recTypeInfos.size > 0) { sobject.RecordTypeId = recTypeInfos.get(sobject.RecordTypeId); @@ -262,4 +270,22 @@ export default class Import extends SfdxCommand { return lookups; } + + private async getValidUserAndQueueList() { + let userQueueSet = new Set(); + + const conn = this.org.getConnection(); + const userResults = (await conn.query('SELECT Id FROM User WHERE IsActive = TRUE')).records as any; + + for (const user of userResults) { + userQueueSet.add(user.Id); + } + + const queueResults = (await conn.query("SELECT Id FROM Group WHERE Type = 'Queue'")).records as any; + + for (const queue of queueResults) { + userQueueSet.add(queue.Id); + } + return userQueueSet; + } } \ No newline at end of file diff --git a/src/commands/texei/data/plan/generate.ts b/src/commands/texei/data/plan/generate.ts index c71db6f..6e63386 100644 --- a/src/commands/texei/data/plan/generate.ts +++ b/src/commands/texei/data/plan/generate.ts @@ -48,7 +48,8 @@ export default class Generate extends SfdxCommand { "name": objectName, "label": "", "filters": "", - "excludedFields": [] + "excludedFields": [], + "exportOwner": false }); }