How to update an incident without creating duplicate incident, import set is completed with errors
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 03:51 AM
Hi Team,
We have an SFTP integration with ServiceNow through schedule jobs (import sets)
once the import set state is completed with errors it will create an incident and the make import log message as short description - we are able to create incident by using Business rule and script Include.
But my client requirement is because of this lot of incidents are created in ServiceNow, they raise a request per instead creating incidents ,update the incidents from 2nd incident onwards for the same day
For the next day first error state create an incident and then from the second incident onwards it needs to update .
Kindly help me on the above issue
Thank you in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2023 08:39 AM
Hi @Dhanu1
You would want to check for an existing incident created that day with the short description 'SFTP Download Job Failure' and with the caller id of '' before creating a new incident. This way if it finds one, that means an initial incident was already created for that day and can be updated with new errors. If not then create a new incident and once the new incident is created thereafter that incident would be the one found and updated with each subsequent errors.
For example something like the following:
...
//made this a function to make reusable
function getLogMessage(runListSysId){
var logMessages = new GlideRecord('import_log');
logMessages.addQuery('run_id', runListSysId); // Assuming the field is named "run_id"
logMessages.orderByDesc('sys_created_on'); // Order by creation date in descending order
logMessages.setLimit(1); // Limit to the most recent log message
logMessages.query();
if (logMessages.next()) {
var formattedLogMessage = logMessages.message.toString();
return'Import Log Message:\n' + formattedLogMessage;
} else {
return = 'No import log message found.';
}
}
while (grRunList.next()) {
if (!grRunList.incident_created) {
//decalare a new incident GlideRecord
var grIncident = new GlideRecord('incident');
//add queries to search for an existing incident with the following data points
//- caller_id = 517a78f3973439507af3baf0f053af77
//- short_description = SFTP download job failure
//- created today
grIncident.addEncodedQuery('caller_id=517a78f3973439507af3baf0f053af77^short_description=SFTP download job failure^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
grIncident.query();
//if a record is found that matches the query update it
if(grIncident.next()){
grIncident.description = getLogMessage(grRunList.sys_id);
grIncident.update();
}else{
//if no initial incident was created today create one
grIncident.initialize();
grIncident.short_description = 'SFTP download job failure';
gs.info('Setting caller_id: ' + grIncident.caller_id);
grIncident.caller_id = '517a78f3973439507af3baf0f053af77';
grIncident.description = getLogMessage(grRunList.sys_id);
grIncident.assignment_group = '47ac8100c3941d10de817305e401317c';
grIncident.business_service = '4b767182c3ebcd10e3c12d4ce0013195';
grIncident.service_offering = '1a9fbee6c3616510eb047305e40131d9';
grIncident.insert();
// Set the incident_created flag to true
grRunList.incident_created = true;
grRunList.update();
}
}
}
...