- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 04-06-2019 08:13 PM
We had come across an requirement to log the integration errors as an attachment and attach the file in the record
1) Call a error script Include every time if the validation failed
1) Create an record in em_event by passing necessary parameters (Could be any table)
2) Process the integration by logging the errors as an attachment in the inserted record in em_event table
-------------------Calling Exception logs script include to process the error-----------------------------------------------------------------
//Call the script include
initialize: function(){
this.exceptionLogs = new WM_ExceptionLogsForIntegration();
}
//Header of the CSV file attachment
var hdr = ["CI Name", "CI Serial","Error", "Error Message"];
this.exceptionLogs.setLogHeaders(hdr);
//Call the scriptInclude function "addLogMessage" every time it loops inside
for loop{
var dataError = [currentItem.node_name, currentItem.serial,"Insert/Update Failed", error_msg];
this.exceptionLogs.addLogMessage(dataError);
}
//Call the final script Include to create an attachment
this.exceptionLogs.submitLoggedMessages('CMDB_XXX_Integration','Script Include - WM_Integration_utils','',this.logSeverity,'JSON API response validation error','Find the attached Error messages','');
------------------------------------ScriptInclude to generate the CSV attachment----------------------------------------------------------
var WM_ExceptionLogsForIntegration = Class.create();
WM_ExceptionLogsForIntegration.prototype = {
//Initialize the values
initialize: function(){
this.logMessages = [];
},
// Sets the headers for a logging function
// Should match number of fields used subsequently for addLogMessage
setLogHeaders: function(hdrArr) {
this.logHeaders = hdrArr;
},
//Integration log messages are pushed into array
// For proper output, expect the same number & order of array fields for every logmessage within one 'run'
addLogMessage: function(logRowArray){
this.logMessages.push(logRowArray);
},
//Process the log messages and convert logs into csv file
//Also calls "createPlatformEvent" function and attaches csv file into inserted record
submitLoggedMessages: function(snSource,snscriptName,MessageKey,severityLevel,title, detailedMessage, additionalPayload) {
var logsPayload = this.logMessages;
var evt = this.createPlatformEvent(snSource,snscriptName,MessageKey,severityLevel,title, detailedMessage, additionalPayload);
var CSVstr = this.createCSV(this.logMessages, this.logHeaders);
gs.print('CSVstr::'+CSVstr);
var insertAttachment = new Attachment();
insertAttachment.write('em_event',evt, 'Integration Error Logs.csv', 'application/csv', CSVstr);
return evt;
},
//Creates the record in event table
createPlatformEvent: function(snSource,snscriptName,MessageKey,severityLevel,title, detailedMessage, additionalPayload){
var createEvent = new GlideRecord("em_event");
createEvent.initialize();
createEvent.source = 'ServiceNow';
createEvent.node = snSource;
createEvent.resource = snscriptName;
createEvent.message_key = MessageKey;
createEvent.severity = severityLevel;
createEvent.description = title+'\r\n'+detailedMessage;
createEvent.additional_info = additionalPayload;
createEvent.time_of_event = gs.nowDateTime();
var eventInsertedSysId = createEvent.insert();
return eventInsertedSysId;
},
// convert an array to a row for CSV file use
arrayToCSVRow: function(row) {
for (var i=0; i < row.length; i++) {
row[i] = row[i].replace('"', '""'); //escape single quotes to double quotes
}
var strRow = '"' + row.join('","') + '"'; // join individual fields with "," and append an " to the end and beginning
return strRow;
},
// Take an array of arrays and convert each array into a row, merge them all into one CSV formatted output
createCSV: function(contentArr, headers ) {
var resultCSV = "";
if (headers) {
resultCSV += this.arrayToCSVRow(headers) + "\r\n";
}
for (var i=0; i < contentArr.length; i++) {
resultCSV += this.arrayToCSVRow(contentArr[i]) + "\r\n";
}
return resultCSV;
},
type: 'WM_ExceptionLogsForIntegration'
};
--------------------------------------------------------------------------------------------
- 712 Views