-
Notifications
You must be signed in to change notification settings - Fork 41
/
upload.ts
91 lines (75 loc) · 3.66 KB
/
upload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {flags, SfdxCommand } from '@salesforce/command';
import localFile2CV = require('../../../../shared/localFile2CV');
import { CreateResult, Record} from './../../../../shared/typeDefs';
export default class Upload extends SfdxCommand {
public static description = 'upload a file from local resources, optionally as a chatter post or attached file on a record';
public static examples = [
`sfdx shane:data:file:upload -f ~/Downloads/King.png
//uploads file from local filesystem as a file
`,
`sfdx shane:data:file:upload -f ~/Downloads/King.png -p 0011900000VkJgrAAF
//uploads file from local filesystem as a file and attaches to a record
`,
`sfdx shane:data:file:upload -f ~/Downloads/King.png -p 0011900000VkJgrAAF -c
//uploads and attaches it to the indicated record, but as a chatter file post
`,
`sfdx shane:data:file:upload -f ~/Downloads/King.png -p 0011900000VkJgrAAF -n CustomName -c
//uploads and attaches it to the indicated record, but as a chatter file post with a name that's not the same name as the local filesystem used
`
];
protected static flagsConfig = {
file: flags.filepath({ char: 'f', description: 'path to file on local filesystem', required: true }),
parentid: flags.id({ char: 'p', description: 'optional record ID that the file should be attached to'}),
chatter: flags.boolean({ char: 'c', description: 'attach as a chatter content post instead of just as a file', dependsOn: ['parentid'] }),
name: flags.string({ char: 'n', description: 'set the name of the uploaded file'})
};
// Comment this out if your command does not require an org username
protected static requiresUsername = true;
// Comment this out if your command does not support a hub org username
// protected static supportsDevhubUsername = true;
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = false;
public async run(): Promise<any> { // tslint:disable-line:no-any
// this.org is guaranteed because requiresUsername=true, as opposed to supportsUsername
const conn = this.org.getConnection();
interface CDLCreateRequest {
ContentDocumentId: string;
LinkedEntityId: string;
ShareType: string;
}
const CV = <Record> await localFile2CV.file2CV(conn, this.flags.file, this.flags.name);
if (!this.flags.parentid) {
this.ux.log(`created file with content document id ${CV.ContentDocumentId}`);
return CV;
} else if (!this.flags.chatter) {
// regular file attachment
this.ux.log(`will create a regular file attachment on record ${this.flags.parentid}`);
const CDLReq = {
ContentDocumentId: CV.ContentDocumentId,
LinkedEntityId: this.flags.parentid,
ShareType: 'V'
} as CDLCreateRequest;
const CDLCreateResult = await conn.sobject('ContentDocumentLink').create(CDLReq) as CreateResult;
if (CDLCreateResult.success) {
this.ux.log(`created regular file attachment on record ${this.flags.parentid}`);
} else {
this.ux.error(CDLCreateResult.message);
}
return CDLCreateResult;
} else {
// chatter post
const feedItemRequest = {
RelatedRecordId: CV.Id,
ParentId: this.flags.parentid,
Type: 'ContentPost'
};
const feedItemCreateResult = await conn.sobject('FeedItem').create(feedItemRequest) as CreateResult;
if (feedItemCreateResult.success) {
this.ux.log(`created chatter file attachment on record ${this.flags.parentid}`);
} else {
this.ux.error(feedItemCreateResult.message);
}
return feedItemCreateResult;
}
}
}