Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Adding Export Owner functionality #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/commands/texei/data/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface DataPlan {
label: string;
filters: string;
excludedFields: Array<string>;
exportOwner: boolean;
}

// Initialize Messages with the current plugin directory
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions src/commands/texei/data/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export default class Import extends SfdxCommand {
const lookups: Array<string> = await this.getLookupsForObject(sobjectName);
let recTypeInfos = new Map<string, string>();

// 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);

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/commands/texei/data/plan/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default class Generate extends SfdxCommand {
"name": objectName,
"label": "",
"filters": "",
"excludedFields": []
"excludedFields": [],
"exportOwner": false
});
}

Expand Down