Skip to content

Commit

Permalink
fix: various data:record bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Dec 3, 2020
1 parent 6b94dd3 commit a05ac07
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-data/messages/record.get.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "displays a single record",
"description": "displays a single record\nSpecify an sObject type and either an ID or a list of <fieldName>=<value> pairs.\nThe format of a field-value pair is <fieldName>=<value>.\nEnclose all field-value pairs in one set of double quotation marks, delimited by spaces.\nEnclose values that contain spaces in single quotes.\n\nTo get data on API performance metrics, specify both --perflog and --json.",
"sObjectType": "the type of the record you’re retrieving",
"sObjectId": "the ID of the record you’re retrieving",
"where": "a list of <fieldName>=<value> pairs to search for",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ export default class Create extends DataCommand {
const result = this.normalize<RecordResult>(await sobject.insert(values));
if (result.success) {
this.ux.log(messages.getMessage('createSuccess', [result.id || 'unknown id']));
this.ux.stopSpinner();
} else {
const errors = this.collectErrorMessages(result);
this.ux.error(messages.getMessage('createFailure', [errors]));
this.ux.stopSpinner('failed');
}
this.ux.stopSpinner();
return result;
}
}
17 changes: 11 additions & 6 deletions packages/plugin-data/src/commands/force/data/record/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import * as os from 'os';

import { flags, FlagsConfig } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { Messages, SfdxError } from '@salesforce/core';
import { Record } from 'jsforce';
import { AnyJson } from '@salesforce/ts-types';
import { DataCommand } from '../../../../dataCommand';
Expand Down Expand Up @@ -58,10 +58,15 @@ export default class Get extends DataCommand {

this.ux.startSpinner('Getting Record');
const sobject = this.getConnection().sobject(this.flags.sobjecttype);
const sObjectId = this.flags.sobjectid || (await this.query(sobject, this.flags.where)).Id;
const result = await sobject.retrieve(sObjectId);
if (!this.flags.json) this.ux.logJson(result);
this.ux.stopSpinner();
return result;
try {
const sObjectId = this.flags.sobjectid || (await this.query(sobject, this.flags.where)).Id;
const result = await sobject.retrieve(sObjectId);
if (!this.flags.json) this.logNestedObject(result);
this.ux.stopSpinner();
return result;
} catch (err) {
this.ux.stopSpinner('failed');
throw new SfdxError(err.name, err.message);
}
}
}
15 changes: 14 additions & 1 deletion packages/plugin-data/src/dataCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { SfdxCommand } from '@salesforce/command';
import { Messages, SfdxError, Org } from '@salesforce/core';
import { AnyJson, Dictionary } from '@salesforce/ts-types';
import { AnyJson, Dictionary, get, Nullable } from '@salesforce/ts-types';
import { BaseConnection, ErrorResult, Record, SObject } from 'jsforce';

Messages.importMessagesDirectory(__dirname);
Expand Down Expand Up @@ -138,6 +138,19 @@ export abstract class DataCommand extends SfdxCommand {
return Array.isArray(results) ? results[0] : results;
}

protected logNestedObject(obj: object, indentation = 0): void {
const space = ' '.repeat(indentation);
Object.keys(obj).forEach((key) => {
const value = get(obj, key, null) as Nullable<string | object>;
if (!!value && typeof value === 'object') {
this.ux.log(`${space}${key}:`);
this.logNestedObject(value, indentation + 2);
} else {
this.ux.log(`${space}${key}: ${value as string}`);
}
});
}

/**
* Takes a sequence of key=value string pairs and produces an object out of them.
* If you repeat the key, it replaces the value with the subsequent value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ describe('force:data:record:get', () => {
.it('should throw an error if values provided to where flag are invalid', (ctx) => {
const result = JSON.parse(ctx.stdout);
expect(result.status).to.equal(1);
expect(result.message).to.equal('Malformed key=value pair for value: Name');
expect(result.name).to.equal('Malformed key=value pair for value: Name');
});
});

0 comments on commit a05ac07

Please sign in to comment.