diff --git a/messages/data.export.md b/messages/data.export.md index ffbb876..377fb73 100644 --- a/messages/data.export.md +++ b/messages/data.export.md @@ -17,3 +17,7 @@ path to data plan file # flags.apitype.summary API Type to use + +# flags.excludenullfields.summary + +Exclude null fields from exported JSON files diff --git a/src/commands/texei/data/export.ts b/src/commands/texei/data/export.ts index aef8be1..c12effd 100644 --- a/src/commands/texei/data/export.ts +++ b/src/commands/texei/data/export.ts @@ -62,6 +62,12 @@ export default class Export extends SfCommand { options: ['rest', 'bulk'], default: 'rest', }), + // new flag to exclude null fields + excludenullfields: Flags.boolean({ + char: 'e', + summary: messages.getMessage('flags.excludenullfields.summary'), + default: false, + }), // loglevel is a no-op, but this flag is added to avoid breaking scripts and warn users who are using it loglevel, }; @@ -113,7 +119,7 @@ export default class Export extends SfCommand { }); const fileName = `${index}-${obj.name}${obj.label ? '-' + obj.label : ''}.json`; - const objectRecords: any = await this.getsObjectRecords(obj, recordIdsMap, flags.apitype); + const objectRecords: any = await this.getsObjectRecords(obj, recordIdsMap, flags.apitype, flags); await this.saveFile(objectRecords, fileName, flags.outputdir); index++; @@ -123,7 +129,12 @@ export default class Export extends SfCommand { return { message: 'Data exported' }; } - private async getsObjectRecords(sobject: DataPlanSObject, recordIdsMap: Map, apitype: string) { + private async getsObjectRecords( + sobject: DataPlanSObject, + recordIdsMap: Map, + apitype: string, + flags: { [key: string]: any } + ) { // Query to retrieve creatable sObject fields let fields: string[] = []; const lookups = []; @@ -300,7 +311,8 @@ export default class Export extends SfCommand { const recordFile: any = {}; recordFile.attributes = objectAttributes; - recordFile.records = recordResults; + + recordFile.records = recordResults.map((record) => removeNullFields(record, flags.excludenullfields)); // eslint-disable-next-line @typescript-eslint/no-unsafe-return return recordFile; @@ -414,3 +426,18 @@ export default class Export extends SfCommand { }); } } + +// add function to remove null values from object +function removeNullFields(record: { [key: string]: any }, excludeNullFields: boolean): { [key: string]: any } { + if (!excludeNullFields) { + return record; + } + + const filteredRecord: { [key: string]: any } = {}; + for (const key in record) { + if (record[key] !== null) { + filteredRecord[key] = record[key]; + } + } + return filteredRecord; +}