Skip to content
This repository was archived by the owner on Nov 5, 2023. It is now read-only.

Commit c0f6ed7

Browse files
author
Mohith Shrivastava
committed
add support for metadata.rename
1 parent c660139 commit c0f6ed7

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,30 @@ EXAMPLES
212212

213213
_See code: [src/commands/deploy/staticresource.ts](https://github.com/msrivastav13/mo-dx-plugin/blob/master/src/commands/deploy/staticresource.ts)_
214214

215+
## `sfdx metadata:rename`
216+
217+
Renames a single metadata using the salesforce Metadata API.
218+
219+
Salesforce allows to rename some types of metadata (CRUD based) using the Metadata API.This commands provides sfdx version
220+
221+
```
222+
USAGE
223+
$ sfdx metadata:rename
224+
225+
OPTIONS
226+
-t, --metadatatype=metadatatype (required) the type of the metadata (Ex CustomObject for customobject, CustomField for custom field)
227+
228+
-o, --oldfullname=oldfullname (required) Full API name of the current component that will be overriden with the new name
229+
230+
-n, --newfullname=newfullname (required) Full API name that will override the existing component name
231+
232+
EXAMPLES
233+
'$ sfdx metadata:rename -t <metadatatype> -n <newname> -o <oldname>',
234+
'$ sfdx metadata:rename -t CustomObject -n MyCustomObject1New__c -o MyCustomObject1__c' // here Custom Object MyCustomObject1__c is renamed to MyCustomObject1New__c
235+
```
236+
237+
_See code: [src/commands/deploy/staticresource.ts](https://github.com/msrivastav13/mo-dx-plugin/blob/master/src/commands/metadata/rename.ts)_
238+
215239

216240

217241
### Important Note When Using these Commands With Non-Scratch Org

messages/org.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"retrieveDxSource": "Retrieves Source is DX format from Unmanaged/Managed package",
99
"retrieveSource": "Retrieves Source from Unmanaged/Managed package in Metadata API format",
1010
"staticresourceDeploy" : "Deploy static resource using tooling api",
11-
"generatePackageXML": "Generates package XML manifest from metadata"
11+
"generatePackageXML": "Generates package XML manifest from metadata",
12+
"renamemetadata" : "Rename metadata using metadata api"
1213
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mo-dx-plugin",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"author": "Mohith Shrivastava",
55
"bugs": "https://github.com/ForceProjects/mo-dx-plugin/issues",
66
"dependencies": {

src/commands/metadata/rename.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { core, flags, SfdxCommand } from '@salesforce/command';
2+
import { AnyJson } from '@salesforce/ts-types';
3+
import chalk from 'chalk';
4+
import { SaveResult } from 'jsforce';
5+
6+
// Initialize Messages with the current plugin directory
7+
core.Messages.importMessagesDirectory(__dirname);
8+
9+
const messages = core.Messages.loadMessages('mo-dx-plugin', 'org');
10+
11+
export default class RenameMetadata extends SfdxCommand {
12+
13+
public static description = messages.getMessage('renamemetadata');
14+
15+
public static examples = [
16+
'$ sfdx metadata:rename -t <metadatatype> -n <newname> -o <oldname>',
17+
'$ sfdx metadata:rename -t CustomObject -n MyCustomObject1New__c -o MyCustomObject1__c'
18+
];
19+
20+
protected static flagsConfig = {
21+
// flag with a value (-n, --name=VALUE)
22+
metadatatype: flags.string({
23+
required: true,
24+
char: 't',
25+
description: 'type of the metadata. Examples are CustomObject, Profile'
26+
}),
27+
newfullname: flags.string({
28+
char: 'n',
29+
required: true,
30+
description: 'new name of the metadata element'
31+
}),
32+
oldfullname: flags.string({
33+
char: 'o',
34+
required: true,
35+
description:
36+
'old name of the metadata element'
37+
})
38+
};
39+
40+
// Comment this out if your command does not require an org username
41+
protected static requiresUsername = true;
42+
43+
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
44+
protected static requiresProject = true;
45+
46+
public async run(): Promise<AnyJson> {
47+
48+
const conn = this.org.getConnection();
49+
let result: Promise<SaveResult>;
50+
try {
51+
result = conn.metadata.rename(this.flags.metadatatype, this.flags.oldfullname, this.flags.newfullname);
52+
result
53+
.then(
54+
res => {
55+
if (res.success) {
56+
console.log(chalk.bold.greenBright(this.flags.metadatatype + ' ' + this.flags.oldfullname + ' is successfully Renamed to ' + this.flags.newfullname + ' ✔'));
57+
}
58+
}
59+
)
60+
.catch(
61+
error => {
62+
console.log('ERROR--' + chalk.bold.redBright(error));
63+
}
64+
);
65+
} catch (ex) {
66+
console.log('ERROR--' + chalk.bold.redBright(ex));
67+
}
68+
return JSON.stringify(result);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)