Skip to content

Instantly share code, notes, and snippets.

@abhididdigi
Last active February 16, 2024 13:57
Show Gist options
  • Save abhididdigi/3dd4c73b4ce0c656ea9b592ae5db2818 to your computer and use it in GitHub Desktop.
Save abhididdigi/3dd4c73b4ce0c656ea9b592ae5db2818 to your computer and use it in GitHub Desktop.
// it expects the following , and if not present, will send out an error.
// msg should have the following properties : table_name, sys_id, column_mapping,column_name.
var POST_Attachment = Class.create();
POST_Attachment.prototype = {
initialize: function(msg) {
this.body = msg;
this.table_name = msg["table_name"];
this.key = msg["key"];
this.base64Encode = msg["base64Encode"];
this.column_name = msg["column_name"];
this.encrypted = msg["encrypted"];
this.file_name = msg["file_name"];
this.content_type = msg["content_type"];
// Only for Kenexa handling.
this.file_type = '';
this.file_column_mapping = '';
JSUtil.logObject(msg, "POST Attachment");
},
// responsible for attaching the file.
attach: function() {
// (1) check for all mandatory fields.
if (JSUtil.nil(this.table_name) || JSUtil.nil(this.key) || JSUtil.nil(this.base64Encode) || JSUtil.nil(this.column_name) || JSUtil.nil(this.encrypted) || JSUtil.nil(this.file_name)) {
return this.prepareMessage("failure", "mandatory_not_found");
}
// (2) get the key's record from ServiceNow.
var record = this.getTableRecord();
//(3) if no such record if found, raise an error.
if (record == -1) {
return this.prepareMessage("failure", "key_error");
}
// write the record.
var document = GlideStringUtil.base64DecodeAsBytes(this.base64Encode);
var attachment = new Attachment();
attachment_sys_id = attachment.write(this.table_name, record.getValue("sys_id"), this.file_name, this.content_type, document);
//take the attachment_sys_id and encrypt it:
if (this.encrypted == "true") {
new OB_EncryptDecryptUtils(attachment_sys_id).encrypt();
}
//return the final status message.
return this.prepareMessage("success", "success");
},
// getting table's record to which the attachment needs to be attached.
getTableRecord: function() {
var gr = new GlideRecord(this.table_name);
gr.addQuery(this.column_name, this.key);
gr.query();
if (gr.next()) {
gs.log("returning sys_id " + gr.getValue("sys_id"));
return gr;
}
return -1;
},
// preparing the error that needs to be sent back.
prepareMessage: function(status, message) {
var o = {};
o["status"] = POST_Attachment.status_code[status];
o["message"] = POST_Attachment.error_messages[message];
return o;
},
get_code: function(file_name) {
// split the file into three pieces and give me the second piece.
var file_name_arr = file_name.split("-");
return file_name_arr[0];
},
extractDateAndCompare: function(file_name1, file_name2) {
var date1 = file_name1.split("-")[2];
var date2 = file_name2.split("-")[2];
if (this.extractDate(date1) > this.extractDate(date2)) {
return file_name1;
} else if (this.extractDate(date1) < this.extractDate(date2)) {
return file_name2;
} else {
return file_name1
}
},
extractDate: function(date) {
var dateObj = new Date(date.replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$4:$5:$6 $2/$3/$1'
));
return dateObj;
},
type: 'POST_Attachment'
};
POST_Attachment.error_messages = {
"mandatory_not_found": "Mandatory columns are missing.",
"success": "Record successfully updated.",
"logSuccess": "Successfully updated error logs.",
"failure": "Record update failed.",
"key_error": "key not found in table.",
"right_attachments_not_found": "The right attachments weren't found in the table."
};
POST_Attachment.status_code = {
"success": 1,
"failure": -1
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment