-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSP_AddFileAttachment.ts
76 lines (66 loc) · 1.64 KB
/
SP_AddFileAttachment.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
/**
*@NApiVersion 2.1
*@NScriptType Restlet
*/
import { EntryPoints } from 'N/types';
import * as record from 'N/record';
import * as file from 'N/file';
import * as error from 'N/error';
/**
* A RESTlet to add a file attachment to a record.
*/
interface PostContext {
recordType: string;
fileType: string;
fileName: string;
fileContents: string;
folder: string;
parentRecordType: string;
parentId: string;
}
function doValidation(args: any, argNames: any, methodName: any) {
for (let i = 0; i < args.length; i++)
if (!args[i] && args[i] !== 0)
throw error.create({
name: 'MISSING_REQ_ARG',
message:
'Missing a required argument: [' +
argNames[i] +
'] for method: ' +
methodName,
});
}
export const post: EntryPoints.RESTlet.post = (context: PostContext) => {
doValidation([context.recordType], ['recordtype'], 'POST');
let fileType: file.Type;
if (context.fileType === 'jpg') {
fileType = file.Type.JPGIMAGE;
} else if (context.fileType === 'png') {
fileType = file.Type.PNGIMAGE;
} else {
fileType = file.Type.PDF;
}
const fileRecord = file.create({
name: context.fileName,
fileType: fileType,
contents: context.fileContents,
encoding: file.Encoding.UTF_8,
folder: Number(context.folder),
isOnline: false,
});
const fileID = fileRecord.save();
if (context.parentId !== null) {
// Attach record
record.attach({
record: {
type: 'file',
id: fileID,
},
to: {
type: context.parentRecordType,
id: context.parentId,
},
});
}
return String(fileID);
};