Skip to content

Commit 5366df6

Browse files
Merge pull request #110 from texei/fix-insert-update
Improved insert/update for records
2 parents 91accc8 + 714dcce commit 5366df6

File tree

4 files changed

+5717
-5823
lines changed

4 files changed

+5717
-5823
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ EXAMPLE
7777
--targetusername texei
7878
```
7979

80-
_See code: [src/commands/texei/contractstatus/value/add.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/contractstatus/value/add.ts)_
80+
_See code: [src/commands/texei/contractstatus/value/add.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/contractstatus/value/add.ts)_
8181

8282
## `sfdx texei:cpqsettings:set -f <string> [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
8383

@@ -106,7 +106,7 @@ EXAMPLE
106106
sfdx texei:cpqsettings:set
107107
```
108108

109-
_See code: [src/commands/texei/cpqsettings/set.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/cpqsettings/set.ts)_
109+
_See code: [src/commands/texei/cpqsettings/set.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/cpqsettings/set.ts)_
110110

111111
## `sfdx texei:data:export -d <string> [-o <string>] [-p <string>] [-a <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
112112

@@ -144,7 +144,7 @@ EXAMPLES
144144
sfdx texei:data:export --dataplan ./data/data-plan.json --outputdir ./data --targetusername texei
145145
```
146146

147-
_See code: [src/commands/texei/data/export.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/data/export.ts)_
147+
_See code: [src/commands/texei/data/export.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/data/export.ts)_
148148

149149
## `sfdx texei:data:import -d <string> [-a] [-o] [-p <string>] [-i] [-u <string>] [--apiversion <string>] [--verbose] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
150150

@@ -190,7 +190,7 @@ EXAMPLE
190190
Data imported!
191191
```
192192

193-
_See code: [src/commands/texei/data/import.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/data/import.ts)_
193+
_See code: [src/commands/texei/data/import.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/data/import.ts)_
194194

195195
## `sfdx texei:data:plan:generate -d <string> -o <string> [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
196196

@@ -217,7 +217,7 @@ EXAMPLE
217217
$ sfdx texei:data:plan:generate --objects Account,Contact,MyCustomObject__c --outputdir ./data
218218
```
219219

220-
_See code: [src/commands/texei/data/plan/generate.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/data/plan/generate.ts)_
220+
_See code: [src/commands/texei/data/plan/generate.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/data/plan/generate.ts)_
221221

222222
## `sfdx texei:debug:lwc:enable [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
223223

@@ -244,7 +244,7 @@ EXAMPLE
244244
sfdx texei:debug:lwc:enable --targetusername myOrg@example.com
245245
```
246246

247-
_See code: [src/commands/texei/debug/lwc/enable.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/debug/lwc/enable.ts)_
247+
_See code: [src/commands/texei/debug/lwc/enable.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/debug/lwc/enable.ts)_
248248

249249
## `sfdx texei:org:contractfieldhistory:fix [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
250250

@@ -272,7 +272,7 @@ EXAMPLE
272272
History tracking fixed.
273273
```
274274

275-
_See code: [src/commands/texei/org/contractfieldhistory/fix.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/org/contractfieldhistory/fix.ts)_
275+
_See code: [src/commands/texei/org/contractfieldhistory/fix.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/org/contractfieldhistory/fix.ts)_
276276

277277
## `sfdx texei:org:shape:extract [-d <string>] [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
278278

@@ -307,7 +307,7 @@ EXAMPLE
307307
$ sfdx texei:org:shape:extract -u bulma@capsulecorp.com
308308
```
309309

310-
_See code: [src/commands/texei/org/shape/extract.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/org/shape/extract.ts)_
310+
_See code: [src/commands/texei/org/shape/extract.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/org/shape/extract.ts)_
311311

312312
## `sfdx texei:package:dependencies:install [-k <string>] [-b <string>] [-p <string>] [-s <string>] [-n <string>] [-w <number>] [-r] [-a <string>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
313313

@@ -370,7 +370,7 @@ EXAMPLE
370370
$ texei:package:dependencies:install -u MyScratchOrg -v MyDevHub -k "1:MyPackage1Key 2: 3:MyPackage3Key" -b "DEV"
371371
```
372372

373-
_See code: [src/commands/texei/package/dependencies/install.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/package/dependencies/install.ts)_
373+
_See code: [src/commands/texei/package/dependencies/install.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/package/dependencies/install.ts)_
374374

375375
## `sfdx texei:picklist:restrict -d <string> [-o] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
376376

@@ -405,7 +405,7 @@ EXAMPLE
405405
$ sfdx texei:picklist:restrict -d my-unrestricted-picklists.json
406406
```
407407

408-
_See code: [src/commands/texei/picklist/restrict.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/picklist/restrict.ts)_
408+
_See code: [src/commands/texei/picklist/restrict.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/picklist/restrict.ts)_
409409

410410
## `sfdx texei:picklist:unrestrict [-o] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
411411

@@ -436,7 +436,7 @@ EXAMPLES
436436
$ sfdx texei:picklist:unrestrict --json > my-unrestricted-picklists.json
437437
```
438438

439-
_See code: [src/commands/texei/picklist/unrestrict.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/picklist/unrestrict.ts)_
439+
_See code: [src/commands/texei/picklist/unrestrict.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/picklist/unrestrict.ts)_
440440

441441
## `sfdx texei:profile:clean [-k <string>] [-p <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
442442

@@ -469,7 +469,7 @@ EXAMPLES
469469
custom-sfdx-source-folder/main/profiles,source-folder-2/main/profiles/myAdmin.profile-meta.xml
470470
```
471471

472-
_See code: [src/commands/texei/profile/clean.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/profile/clean.ts)_
472+
_See code: [src/commands/texei/profile/clean.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/profile/clean.ts)_
473473

474474
## `sfdx texei:sharedactivities:enable [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
475475

@@ -496,7 +496,7 @@ EXAMPLE
496496
$ sfdx texei:sharedactivities:enable
497497
```
498498

499-
_See code: [src/commands/texei/sharedactivities/enable.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/sharedactivities/enable.ts)_
499+
_See code: [src/commands/texei/sharedactivities/enable.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/sharedactivities/enable.ts)_
500500

501501
## `sfdx texei:sharingcalc:recalculate [-s <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
502502

@@ -527,7 +527,7 @@ EXAMPLE
527527
Recalculated Sharing Rules
528528
```
529529

530-
_See code: [src/commands/texei/sharingcalc/recalculate.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/sharingcalc/recalculate.ts)_
530+
_See code: [src/commands/texei/sharingcalc/recalculate.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/sharingcalc/recalculate.ts)_
531531

532532
## `sfdx texei:sharingcalc:resume [-s <string>] [-t <number>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
533533

@@ -561,7 +561,7 @@ EXAMPLE
561561
Sharing calculations resumed
562562
```
563563

564-
_See code: [src/commands/texei/sharingcalc/resume.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/sharingcalc/resume.ts)_
564+
_See code: [src/commands/texei/sharingcalc/resume.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/sharingcalc/resume.ts)_
565565

566566
## `sfdx texei:sharingcalc:suspend [-s <string>] [-t <number>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
567567

@@ -595,7 +595,7 @@ EXAMPLE
595595
Sharing calculations suspended
596596
```
597597

598-
_See code: [src/commands/texei/sharingcalc/suspend.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/sharingcalc/suspend.ts)_
598+
_See code: [src/commands/texei/sharingcalc/suspend.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/sharingcalc/suspend.ts)_
599599

600600
## `sfdx texei:skinnyprofile:check [-p <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
601601

@@ -619,7 +619,7 @@ EXAMPLE
619619
$ texei:skinnyprofile:check
620620
```
621621

622-
_See code: [src/commands/texei/skinnyprofile/check.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/skinnyprofile/check.ts)_
622+
_See code: [src/commands/texei/skinnyprofile/check.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/skinnyprofile/check.ts)_
623623

624624
## `sfdx texei:skinnyprofile:retrieve [-t <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
625625

@@ -649,7 +649,7 @@ EXAMPLE
649649
$ texei:skinnyprofile:retrieve -u MyScratchOrg
650650
```
651651

652-
_See code: [src/commands/texei/skinnyprofile/retrieve.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/skinnyprofile/retrieve.ts)_
652+
_See code: [src/commands/texei/skinnyprofile/retrieve.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/skinnyprofile/retrieve.ts)_
653653

654654
## `sfdx texei:source:customlabel:replace -l <string> -v <string> [-p <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
655655

@@ -673,7 +673,7 @@ EXAMPLE
673673
$ texei:source:customlabel:replace --label GreatSalesforceBlog --value https://blog.texei.com
674674
```
675675

676-
_See code: [src/commands/texei/source/customlabel/replace.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/source/customlabel/replace.ts)_
676+
_See code: [src/commands/texei/source/customlabel/replace.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/source/customlabel/replace.ts)_
677677

678678
## `sfdx texei:source:layouts:cleanorg [-p <string>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
679679

@@ -706,7 +706,7 @@ EXAMPLES
706706
$ texei:source:layouts:cleanorg --targetusername myScratchOrg --targetdevhubusername myDevHub
707707
```
708708

709-
_See code: [src/commands/texei/source/layouts/cleanorg.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/source/layouts/cleanorg.ts)_
709+
_See code: [src/commands/texei/source/layouts/cleanorg.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/source/layouts/cleanorg.ts)_
710710

711711
## `sfdx texei:user:update [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
712712

@@ -739,5 +739,5 @@ EXAMPLES
739739
sfdx texei:user:update --values "LanguageLocaleKey=en_US UserPermissionsMarketingUser=true" --json
740740
```
741741

742-
_See code: [src/commands/texei/user/update.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.3/src/commands/texei/user/update.ts)_
742+
_See code: [src/commands/texei/user/update.ts](https://github.com/texei/texei-sfdx-plugin/blob/v1.16.4/src/commands/texei/user/update.ts)_
743743
<!-- commandsstop -->

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "texei-sfdx-plugin",
33
"description": "Texeï's plugin for sfdx",
4-
"version": "1.16.3",
4+
"version": "1.16.4",
55
"author": "Texeï",
66
"bugs": "https://github.com/https://github.com/texei/texei-sfdx-plugin/issues",
77
"dependencies": {

src/commands/texei/data/import.ts

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -327,47 +327,66 @@ export default class Import extends SfdxCommand {
327327
sobjectsResult.push(...chunkResults);
328328
}
329329
}
330-
else if (records[0] && records[0].Id) {
331-
// There is an Id, so it's an update
332-
this.debug(`DEBUG updating ${sobjectName} records`);
333-
334-
// Checking if a batch size is specified
335-
const batchSize = batchSizeMap.get(dataFileName) ? batchSizeMap.get(dataFileName) : 200;
336-
for (var i = 0; i < records.length; i += batchSize) {
337-
// @ts-ignore: Don't know why, but TypeScript doesn't use the correct method override
338-
const chunkResults: RecordResult[] = await conn.sobject(sobjectName).update(records.slice(i, i + batchSize), { allowRecursive: true, allOrNone: this.flags.allornone })
339-
.catch(err => {
340-
if (this.flags.ignoreerrors) {
341-
this.ux.log(`Error importing records: ${err}`);
342-
}
343-
else {
344-
throw new SfdxError(`Error importing records: ${err}`);
345-
}
346-
});
347-
sobjectsResult.push(...chunkResults);
348-
}
349-
}
350330
else {
351-
// No Id, insert
352-
this.debug(`DEBUG inserting ${sobjectName} records`);
331+
if (records && records.length > 0) {
332+
let recordsToInsert:Array<any> = new Array<any>();
333+
let recordsToUpdate:Array<any> = new Array<any>();
334+
for (const record of records) {
335+
if (record.Id) {
336+
// There is an Id, so it's an update
337+
recordsToUpdate.push(record);
338+
}
339+
else {
340+
// No Id, insert record
341+
recordsToInsert.push(record);
342+
}
343+
}
353344

354-
// Checking if a batch size is specified
355-
const batchSize = batchSizeMap.get(dataFileName) ? batchSizeMap.get(dataFileName) : 200;
356-
for (var i = 0; i < records.length; i += batchSize) {
357-
// @ts-ignore: Don't know why, but TypeScript doesn't use the correct method override
358-
const chunkResults: RecordResult[] = await conn.sobject(sobjectName).insert(records.slice(i, i + batchSize), { allowRecursive: true, allOrNone: this.flags.allornone })
359-
.catch(err => {
360-
if (this.flags.ignoreerrors) {
361-
this.ux.log(`Error importing records: ${err}`);
362-
}
363-
else {
364-
throw new SfdxError(`Error importing records: ${err}`);
365-
}
366-
});
367-
sobjectsResult.push(...chunkResults);
345+
// UPDATING RECORDS
346+
if (recordsToUpdate.length > 0) {
347+
this.debug(`DEBUG updating ${sobjectName} records`);
348+
349+
// Checking if a batch size is specified
350+
const batchSize = batchSizeMap.get(dataFileName) ? batchSizeMap.get(dataFileName) : 200;
351+
for (var i = 0; i < recordsToUpdate.length; i += batchSize) {
352+
// @ts-ignore: Don't know why, but TypeScript doesn't use the correct method override
353+
const chunkResults: RecordResult[] = await conn.sobject(sobjectName).update(recordsToUpdate.slice(i, i + batchSize), { allowRecursive: true, allOrNone: this.flags.allornone })
354+
.catch(err => {
355+
if (this.flags.ignoreerrors) {
356+
this.ux.log(`Error importing records: ${err}`);
357+
}
358+
else {
359+
throw new SfdxError(`Error importing records: ${err}`);
360+
}
361+
});
362+
sobjectsResult.push(...chunkResults);
363+
}
364+
}
365+
366+
// INSERTING RECORDS
367+
if (recordsToInsert.length > 0) {
368+
// No Id, insert
369+
this.debug(`DEBUG inserting ${sobjectName} records`);
370+
371+
// Checking if a batch size is specified
372+
const batchSize = batchSizeMap.get(dataFileName) ? batchSizeMap.get(dataFileName) : 200;
373+
for (var i = 0; i < recordsToInsert.length; i += batchSize) {
374+
// @ts-ignore: Don't know why, but TypeScript doesn't use the correct method override
375+
const chunkResults: RecordResult[] = await conn.sobject(sobjectName).insert(recordsToInsert.slice(i, i + batchSize), { allowRecursive: true, allOrNone: this.flags.allornone })
376+
.catch(err => {
377+
if (this.flags.ignoreerrors) {
378+
this.ux.log(`Error importing records: ${err}`);
379+
}
380+
else {
381+
throw new SfdxError(`Error importing records: ${err}`);
382+
}
383+
});
384+
sobjectsResult.push(...chunkResults);
385+
}
386+
}
368387
}
369388
}
370-
389+
371390
// Some errors are part of RecordResult but don't throw an exception
372391
for (let i = 0; i < sobjectsResult.length; i++) {
373392

0 commit comments

Comments
 (0)