You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 15 Next »
This server class contains methods for operating the attachments..
SimpleAttachment()
Use this constructor to create an empty SimpleAttachment object.
const attach = new SimpleAttachment();
base64Decode(data)
This method returns an ASCII string decoded from the base64 string specified.
Parameters:
Name | Type | Mandatory | Default Value |
---|---|---|---|
data | String | Y | N |
strict | Boolean | N | true |
If the input data contains symbols that are not in the base64 alphabet, and the strict parameter is set to true, the method base64_decode() returens false. If the strict parameter is set to false, the symbols that are not in base64, will be omited.
Return:
Type | Description |
---|---|
String | A decoded string |
Example:
const attach = new SimpleAttachment(); const result = attach.base64Decode('TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ='); ss.info(result); // Info: Lorem ipsum dolor sit amet
base64Encode(data)
This method returns a base64 string from the string specified.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
data | String | Y | N |
Return:
Type | Description |
---|---|
String | An encoded base64 string |
Example:
const attach = new SimpleAttachment(); const result = attach.base64Encode('Lorem ipsum dolor sit amet'); ss.info(result); // Info: TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ=
copy(sourceTableName, sourceID, targetTableName, targetID)
Use this method to copy attachments from the source record to the target record.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
sourceTableName | String | Y | N |
sourceID | String | Y | N |
targetTableName | String | Y | N |
targetID | String | Y | N |
Return:
Type | Description |
---|---|
Boolean | This method returns true if attachments have been copied successfully; otherwise, it returns false. |
Example:
const attach = new SimpleAttachment(); attach.copy('sys_email', '155964310500000059', 'task', current.sys_id);
deleteAttachment(attachmentID)
This method deletes the specified attachment.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
attachmentID | String | Y | N |
Return:
Type | Description |
---|---|
Void | This method does not return a value. |
Example:
const attach = new SimpleAttachment(); attach.deleteAttachment('157052637119478714');
getAttachmentUrlById(attachmentId)
Use this method to get the URL of the specified attachment in the cloud storage.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
attachmentId | string | Y | N |
Return:
Type | Description |
---|---|
String | The attachment URL in the cloud storage |
Example:
const ATTACH_ID = '163553718313772587'; const simpleAttach = new SimpleAttachment(); const attachUrl = simpleAttach.getAttachmentUrlById(ATTACH_ID); ss.info(attachUrl); // Info: https://s3-{your-instance-url}/public-attachment/5/32/9bnc2pcb3axyfatgtc6lsi7...
getContent(sysAttachment)
This method gets the attached content as a string in the Content field. The field is filled automatically if the file is of type TXT.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
sysAttachment | SimpleRecord | Y | N |
Return:
Type | Description |
---|---|
String | The attachment content as a string if the file is txt. If the field is empty or the attchment is of another format, the method return null. |
Example:
const attach = new SimpleAttachment(); ss.info(attach.getContent('168109939701169218')); // Info: task active additional_comments approval_state ...
readBase64(attachmentId)
Use this method to get an encoded string from the specified attachment. Together with the writeBase64() method, you can use it to copy attachments.
The attachment size is limited up to 10 Mb; otherwise, the method returns an emtpy string, and the following exception: File size exceeded allowed limit appears in the Exception ogs (sys_log_exception) table.
Parameter(s):
Name | Type | Mandatory | Default value |
---|---|---|---|
attachmentId | String | Y | N |
Return:
Type | Description |
---|---|
String | A base64-encoded string. If the file is too big, the return value can be empty. |
const read = new SimpleAttachment(); ss.info(read.readBase64('168025458107121347')); // Info: 0YLQtdGB0YLQvtCy0YvQuSDQtNC+0LrRg9C80LXQvdGC
rename(attachmentId, fileName)
With this method you can rename an attachment and its extension.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
attachmentId | String | Y | N |
fileName | String | Y | N |
Return:
Type | Description |
---|---|
Void | This method does not return a value. |
Example:
const DOC_ID = ss.getDocIdByIds(current.sys_db_table_id, current.sys_id); const simpleAttach = new SimpleAttachment(); const attachRecord = new SimpleRecord('sys_attachment'); attachRecord.addQuery('record_document_id', DOC_ID); attachRecord.addQuery('mime_content_type', 'application/json'); attachRecord.selectAttributes('sys_id'); attachRecord.query(); attachRecord.next(); simpleAttach.rename(attachRecord.sys_id, `${current.number} - ${current.subject}.json`);
write(documentId, fileName, content, mimeContentType)
Use this method to create and add an attachment to the record specified.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
documentId | String | Y | N |
filename | String | Y | N |
content | String | Y | N |
mimeContentType | String | Y | N |
Return:
Type | Description |
---|---|
String | The ID of the attachment; in case of an error, it returns null. |
Example:
const simpleAttach = new SimpleAttachment(); const attachID = simpleAttach.write( ss.getDocIdByIds(current.sys_db_table_id, current.sys_id), 'readme.json', JSON.stringify(current.getAttributes(), null, '\t'), 'application/json' );
writeBase64(documentId, fileName, base64, mimeContentType)
WIth this method you cab create and add an attachment encoded with the base64 to the record specified. Together with the readBase64() method, you can use it to copy attachments.
Parameter(s):
Name | Type | Mandatory | Default Value |
---|---|---|---|
documentID | String | Y | N |
fileName | String | Y | N |
base64 | String | Y | N |
mimeContentType | String | Y | N |
Return:
Type | Description |
---|---|
String or NULL | The ID of the attachment; in case of an error, it returns null. |
Example:
const CURRENT_USER_DOC_ID = ss.getDocIdByIds(ss.getUser().sys_db_table_id, ss.getUserID()); const simpleAttach = new SimpleAttachment(); const base64Value = 'R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs='; const attachId = simpleAttach.writeBase64( CURRENT_USER_DOC_ID, 'file_example.gif', base64Value, 'image/gif' ); ss.info(attachId); //Info: 162245472311776172
- No labels